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

Creating and managing threads, using mutexes and other synchronization primitives.

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

Threadpools

Using thread pools for efficient task execution.

Multithreading with the crossbeam Crate

Message Passing and Channels

Communicating between threads using channels.

Shared State

Shared Memory. Using atomic types for thread-safe data access.

Concurrent Data Structures

Data Parallelism

Using iterators in parallel with crates like rayon.

Send and Sync

  • Async.
  • Async and Blocking.
  • Async Channels.
  • Concurrent Data Structures.
  • Message Passing.
  • Explicit Threads.
  • Processor.

References