Concurrency vs Parallelism#
Concurrency and parallelism are related but distinct concepts in computer science.
- Rob Pike famously stated: “Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.”
- Katherine Cox-Buday clearly noted in Concurrency in Go: “Concurrency is a property of the code; parallelism is a property of the running program.”
Concurrency#
Concurrency is the composition of independently executing processes. It is a way to structure a program by breaking it into pieces that can be executed independently.
- Focus: Structure and management of multiple tasks.
- Mechanism: Time-slicing or context switching on a single core, or distribution across multiple cores.
- Goal: To handle multiple events or tasks that may overlap in time, improving responsiveness.
Parallelism#
Parallelism is the simultaneous execution of multiple computations. it requires hardware with multiple processing units (eg. multi-core CPU, GPU).
- Focus: Performance and speed through simultaneous execution.
- Mechanism: Running different parts of a task or different tasks at the same time on different cores.
- Goal: To increase throughput and reduce total execution time for computationally intensive tasks.
Comparison#
| Feature | Concurrency | Parallelism |
|---|---|---|
| Definition | Dealing with multiple tasks at once | Doing multiple tasks at once |
| Focus | Structure / Management | Execution / Speed |
| Hardware | Can run on a single core | Requires multiple cores |
| Relationship | A way to structure a program | A way to execute a program |
| Analogy | Multiple people waiting in lines for one teller | Multiple tellers serving multiple lines |
Visual Representation#
graph TD
subgraph "Concurrency (1 Core)"
C1[Task A] --> C2[Task B]
C2 --> C1
end
subgraph "Parallelism (2 Cores)"
P1[Core 1: Task A]
P2[Core 2: Task B]
end
Key Takeaway#
- Single Core (1 Core): Concurrency (O), Parallelism (X)
Multiple tasks are interleaved via time-slicing and context switching, dealing with tasks concurrently, but only one CPU instruction is executed physically at any given instant. - Multi Cores (Multiple Cores): Concurrency (O), Parallelism (O)
Tasks structured concurrently are scheduled onto multiple CPU cores physically, executing simultaneously.
You can have concurrency without parallelism (on a single core), and you can have parallelism without concurrency (eg. bit-level parallelism). However, concurrency is often a prerequisite for effective parallelism in software. No matter how sophisticated a runtime scheduler is, it cannot synthesize physical parallelism beyond hardware limits.