8 Concurrency and Parallelism

CONCURRENCYREVIEWED: 2026-09-13

You understand R but have not written concurrent code. This section teaches how parallel systems work and which hazards you must handle in concurrent R code.

The concepts here apply to any parallel language. You need them to understand what R is missing, and what any solution must add.

After this section

  • Explain the difference between processes and threads
  • Identify and fix a race condition
  • Describe what a mutex does and when it is necessary
  • Design a thread pool and reason about load balancing
  • Connect each primitive to a specific R parallelism problem

The pages

  1. Processes vs Threads. Two different strategies for parallel execution and what each costs.
  2. Shared Memory. How threads see the same memory and why that causes problems.
  3. Race Conditions. When two threads interfere with each other and how to spot it.
  4. Mutexes. Locks that serialize access to shared state.
  5. Atomics. CPU primitives for coordination without locking.
  6. Thread Pools. A queue of tasks and a fixed number of worker threads.
  7. Data Parallelism vs Task Parallelism. Same operation over partitioned data or different operations concurrently.
  8. Message Passing and Channels. Thread communication without shared mutable state.
  9. Condition Variables and Semaphores. Waiting for state changes and counting permits.
  10. Deadlock, Livelock, and Starvation. Three failure modes and how to prevent each.

Before this section

Read Orientation first.

Where this leads

Scheduling explains how runtime schedulers distribute work. IPC and Data Movement covers the costs of moving data between processes and threads. Multi-Interpreter Architecture shows how my work applies these primitives to R.

Three topics have no page of their own. Lock-free algorithms are covered in Atomics. Actors and message-driven coordination are covered in Message Passing and Channels and in Shared Memory vs Message Passing. Fork-join and task graphs are covered in part by Thread Pools and Work Stealing.

Back to top