Concurrency · foundation

Concurrency vs parallelism

Concurrency is the composition of tasks whose lifetimes overlap and may interleave; parallelism is the simultaneous execution of work on multiple processing resources.

Why it matters

Concurrent code has ordering and coordination problems even on one core, while parallel speedup additionally depends on divisible work, hardware, contention, and overhead.

Mental model

How to reason about concurrency vs parallelism

Concurrency asks how several in-progress tasks take turns and communicate. Parallelism asks how many pieces are physically executing at the same instant; a system may have either, both, or neither.

Analogy

One cook alternates between simmering soup and chopping vegetables concurrently; two cooks chopping at the same moment work in parallel.

Examples

See the boundary, not just the happy path

Worked example · Single-core event loop

task A runs, awaits I/O; task B runs; A resumes

The tasks overlap in lifetime and interleave, so execution is concurrent even though only one callback runs at a time.

Worked example · Parallel computation

four workers process four independent image tiles on four cores

The tiles can execute simultaneously because the workload is divided across processing resources.

Useful contrast · Parallel but coordinated

workers update a shared result table

Parallel execution does not remove concurrency concerns; shared mutations still need a correct coordination strategy.

Common mistakes

Misconceptions to remove early

Using the words as synonyms

Overlapping task structure and simultaneous hardware execution are different properties, with different correctness and performance questions.

Expecting more workers to guarantee speedup

Serial portions, scheduling, communication, cache contention, and limited cores can erase or reverse a parallel gain.

Quick check

Can you predict the result?

1. Can two tasks be concurrent on a single CPU core?
  • Yes; their executions can interleave while their lifetimes overlap
  • No; concurrency requires two physical cores
  • Only if they never wait for I/O
Answer: Yes; their executions can interleave while their lifetimes overlap
2. Why can parallel code still have race conditions?
Answer: Simultaneously executing workers may access shared state with an ordering that is not correctly coordinated.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster