Concurrency

cat-concurrency

This section covers concurrent programming, and specifically parallel programming.

Parallelism implies:

  • True simultaneous execution of multiple tasks on multiple cores or processors.
  • Mechanism: uses operating system threads.
  • Important for CPU-heavy computations.
  • Often requires explicit management of threads and thread pools.
  • Requires careful synchronization to prevent data races (using mechanisms like mutexes or atomics).
  • Overhead due to thread creation and switching.

Key constructs in Rust:

  • Threads are independent units of execution that can be spawned using e.g. std::thread::spawn.
  • Mutexes e.g. std::sync::Mutex protect shared data from race conditions.
  • Channels e.g. std::sync::mpsc allow threads to communicate and exchange data.

Explicit threads

RecipeCratesCategories
Use spawn, joinstd[cat-concurrency]
Use scoped threadsstdcat-concurrency

Threadpools

Multithreading with the crossbeam crate

Message passing and channels

Shared state

Concurrent data structures

Data parallelism

Send and Sync

RecipeCratesCategories
Send and Sync traitsstdcat-concurrency

See also

Rust concurrency book