flowchart LR
subgraph design["properties"]
conc["concurrency:<br/>independent tasks"]
para["parallelism:<br/>same-instant execution"]
end
subgraph mech["mechanisms"]
async["async I/O<br/>(one thread)"]
mt["multithreading<br/>(BLAS threads)"]
mp["multiprocessing<br/>(R PSOCK cluster)"]
end
conc -.-> async & mt & mp
para -.-> mt & mp
Concurrency vs Parallelism vs Multithreading
Concurrency, parallelism and multithreading are often used as if they were one idea. Each names something different, and choosing how to speed up an R job depends on keeping them separate.
Three definitions
A task is a unit of work that can progress independently. Think of a single statistical model fit or a database query.
A worker is something that executes a task. A worker can be a CPU core, a thread, a process, or even a lightweight coroutine.
A thread is a single execution context within one process. Threads in the same process share memory.
A process is a running program with its own private address space. Processes do not share memory unless they set it up explicitly.
Concurrency and parallelism are independent of each other, and a program can have either one without the other.
Concurrency is a design property. It means the program is structured as tasks that progress independently. On a single core, a concurrent program interleaves tasks in time. When one task waits (for I/O or a lock), the scheduler runs another. The program is concurrent in structure even though nothing is parallel in fact.
Parallelism is a hardware property. It means tasks execute at the same time on different cores. Parallelism requires hardware that can do more than one thing at the same instant, usually several cores. It does not require concurrent design. You can write a parallel program that looks like sequential code and have the compiler or hardware handle the details.
Multithreading is a mechanism. It is one way to get both concurrency (by interleaving task execution on one core) and parallelism (by running different threads on different cores). Threads share memory, so handing data from one thread to another costs almost nothing. The same sharing makes threads the easiest of these mechanisms to get wrong, because threads that touch shared memory must synchronize.
Multiprocessing is another mechanism. It gets parallelism by running multiple processes. Each process has its own address space and its own heap, so ordinary memory in one process cannot take part in a data race with another. The tradeoff is overhead: creating a process is expensive compared to creating a thread, and sending data between processes usually means copying it, unless the processes map the same memory.
Async is a third mechanism. It is concurrency without threads. Async code is structured as coroutines that yield control when they wait for I/O, and a single thread runs many coroutines by switching to another one whenever one waits. It suits I/O-bound tasks. On one thread it gives no parallelism, though a multithreaded runtime such as Tokio’s default one runs its tasks on a thread pool.
Four examples
Parallel R code runs in several processes. parallel::mclapply() forks the R session on Linux and macOS, so each worker starts with a copy-on-write view of the parent’s memory and sends its result back serialized. parallel::parLapply() runs on a cluster instead, and the default PSOCK cluster starts fresh R processes on every platform: tasks and results both travel serialized over sockets.
Either way each worker has its own address space and its own interpreter. No R object is shared between workers, so nothing needs a lock.
Concurrency is a property of the design and parallelism a property of the hardware. Multithreading and multiprocessing can deliver both; async I/O delivers concurrency only.
BLAS threads
Many R users already run multithreaded code without knowing it. When R is linked to an optimized BLAS library such as OpenBLAS or Intel MKL, a large matrix product (%*%, crossprod()) runs on several threads. They share the process’s memory and work on the same matrix. R’s bundled reference BLAS is single-threaded.
This is safe because the library divides the work so that its threads write separate parts of the result, and none of them touches the interpreter. R code sees one call that returns a matrix, and the interpreter itself stays on one thread.
Work-stealing pools
A work-stealing thread pool is a multithreading pattern. A fixed set of threads, the workers, each keep a double-ended queue of tasks. In the classic design, used by Cilk and Rayon, a worker pushes new tasks onto one end of its own queue and takes its next task from that same end. Go and Tokio keep a FIFO queue per worker, where the owner and a thief take from the same end. A worker whose queue runs empty steals a task from another worker’s queue (from the other end, in the classic design), so idle threads find work without one central queue that every thread contends for.
The pool does its own synchronization, so task code that shares no data needs no locks.
Async I/O
Suppose you need to fetch data from ten web services. You write ten async functions. Each function waits for a response. While one waits, the scheduler runs another. One thread executes all ten functions by switching between them when they block.
This is concurrency. Each function is independent. But nothing runs in parallel. It is faster than sequential I/O because time spent waiting for network is reused for other work.
Consequences for R
R code has no way to start a thread. Parallel R code therefore runs in several processes, and threads appear only inside native code such as a multithreaded BLAS or data.table’s C code. Processes are safe, but each worker pays to start, to load its packages, and to copy its data in and out.
threaded-r runs several interpreters on separate threads of one process, each interpreter with its own state. The aim is to keep the isolation that processes give while paying less to start workers and to hold their memory.
Threads are not the only answer. Async suits I/O-bound code, and separate processes remain the safest choice when workers must be fully isolated from each other.
Read next: processes vs threads covers the tradeoff in detail. Thread pools covers the pool pattern, and work stealing the scheduler that balances it.
Further reading
- Sutter, H. (2005). The free lunch is over: A fundamental turn toward concurrency in software <http://www.gotw.ca/publications/concurrency-ddj.htm>. Dr. Dobb’s Journal, 30(3).
- The Rust Book: Fearless Concurrency <https://doc.rust-lang.org/book/ch16-00-concurrency.html>
- Python PEP 703: Making the Global Interpreter Lock Optional in CPython <https://peps.python.org/pep-0703/>
Where this appears
- threaded-r Brings multithreading to R, mixing concurrency and parallelism within one process.
- sunlight Uses multiprocessing (forks) to parallelize tasks across address spaces.