22.2 Concurrency vs. True Parallelism

Although concurrency and parallelism are closely related, they have distinct meanings:

  • Concurrent Execution: Multiple tasks appear to run at the same time, even on a single CPU core. The operating system switches among tasks rapidly.
  • Parallel Execution: Multiple tasks actually run simultaneously on different CPU cores or hardware threads.

Whether tasks run concurrently or in parallel depends on:

  1. The number of available CPU cores
  2. The operating system's scheduling decisions
  3. The nature of the tasks (CPU-bound vs. I/O-bound)

Rust provides abstractions for both scenarios. You can use threads, async functions, or libraries like Rayon to ensure your programs utilize all available hardware resources efficiently.