Concurrency & Parallelism
How do multiple pieces of work make progress safely, how do we coordinate shared state, how do we use multiple cores effectively, and why do concurrent systems fail in ways that testing does not catch?
Your path through Concurrency & Parallelism
Start with these 5 lessons
Then follow the stages in order
Each stage builds on the ones before it. Labs and simulators sit right after the lessons they exercise. Open a stage to see its lessons. Full roadmap with goals →
Concurrency, parallelism, tasks and threads0 of 16 lessons done
- What Concurrency Actually IsFoundation≈ 15 min
- Which One Does This Workload Need?Foundation≈ 15 min
- Why Concurrency Exists: WaitingFoundation≈ 15 min
- Why Parallelism Exists: ComputeFoundation≈ 15 min
- Classifying the Work: Computing or Waiting?Foundation≈ 15 min
- Choosing an Execution ModelFoundation≈ 15 min
- What "Making Progress" Actually MeansFoundation≈ 15 min
- Concurrency Is Always Bought With ComplexityFoundation≈ 20 min
- Processes: Isolation You Cannot Accidentally BreakFoundation≈ 15 min
- Threads: One Address Space, Several Instruction StreamsFoundation≈ 20 min
- Thread or Process?Foundation≈ 15 min
- A Task Is Not a ThreadFoundation≈ 15 min
- Coroutines: Functions That Can PauseFoundation≈ 15 min
- Python: Threads, Processes and the GILFoundation≈ 20 min
- JavaScript: One Event Loop per Agent, Not One Thread per RuntimeFoundation≈ 20 min
- C++: Threads, Atomics and a Memory Model With TeethFoundation≈ 20 min
Shared state, races and critical sections0 of 15 lessons done
- Shared Mutable StateFoundation≈ 15 min
- Interleavings: The Schedule Is Part of the ProgramFoundation≈ 20 min
- Invariants: Name It Before You Lock ItFoundation≈ 15 min
- Finding the Critical SectionFoundation≈ 20 min
- Reasoning About Races: A Method, Not an InstinctFoundation≈ 20 min
- Data Race Is Not Race ConditionFoundation≈ 20 min
- The Atomicity IllusionFoundation≈ 20 min
- Nondeterminism: Same Input, Different OutputFoundation≈ 15 min
- Immutability as a Concurrency StrategyFoundation≈ 15 min
- Copy or Share?Foundation≈ 10 min
- Copy-on-Write as a Concurrency StrategyFoundation≈ 15 min
- Optimistic Concurrency ControlFoundation≈ 20 min
- Pessimistic ConcurrencyFoundation≈ 15 min
- Optimistic vs PessimisticFoundation≈ 15 min
- Initialization RacesFoundation≈ 20 min
Mutexes, semaphores and condition variables0 of 11 lessons done
- Mutexes: What They Protect and What They Do NotFoundation≈ 20 min
- Lock Scope: What You Hold It AcrossFoundation≈ 20 min
- Read/Write Locks, HonestlyFoundation≈ 20 min
- Semaphores: Counting Permits as a Resource LimitFoundation≈ 20 min
- Semaphore versus Mutex: Not the Same PrimitiveFoundation≈ 20 min
- Condition Variables: Waiting Until a Predicate Is TrueFoundation≈ 20 min
- Lost Wakeups: The Notify That Arrived Before the WaitFoundation≈ 20 min
- Spurious Wakeups: Why It Is `while`, Not `if`Foundation≈ 20 min
- Spin LocksFoundation≈ 20 min
- What "Thread-Safe" Actually MeansFoundation≈ 20 min
- ReentrancyFoundation≈ 15 min
Async, promises, coroutines and event loops0 of 16 lessons done
- Event Loops as a Concurrency ModelIntermediate≈ 15 min
- Await Is a Yield PointIntermediate≈ 15 min
- Futures & PromisesIntermediate≈ 15 min
- Worker ThreadsIntermediate≈ 15 min
- Web WorkersIntermediate≈ 15 min
- Event Loop or Threads?Intermediate≈ 20 min
- Async Is Not ParallelismIntermediate≈ 15 min
- Blocking the Event LoopIntermediate≈ 20 min
- Promise.all & gatherIntermediate≈ 20 min
- The Sequential Await TrapIntermediate≈ 20 min
- Structured ConcurrencyIntermediate≈ 15 min
- CancellationIntermediate≈ 20 min
- Cancellation PropagationIntermediate≈ 20 min
- TimeoutsIntermediate≈ 15 min
- Deadlines vs TimeoutsIntermediate≈ 15 min
- Orphaned TasksIntermediate≈ 15 min
Thread pools, worker pools, producer/consumer and backpressure0 of 17 lessons done
- Producer / ConsumerIntermediate≈ 15 min
- Bounded vs Unbounded QueuesIntermediate≈ 20 min
- BackpressureIntermediate≈ 15 min
- ChannelsIntermediate≈ 15 min
- Message PassingIntermediate≈ 15 min
- The Actor ModelIntermediate≈ 15 min
- Concurrent QueuesIntermediate≈ 15 min
- Draining a PipelineIntermediate≈ 15 min
- Thread PoolsIntermediate≈ 15 min
- Sizing a Thread PoolIntermediate≈ 15 min
- Worker Pools Beyond ThreadsIntermediate≈ 15 min
- Work StealingIntermediate≈ 15 min
- Pool SaturationIntermediate≈ 20 min
- Bounding ConcurrencyIntermediate≈ 20 min
- Unbounded ConcurrencyIntermediate≈ 20 min
- Thundering HerdIntermediate≈ 20 min
- Single-Flight CoalescingIntermediate≈ 20 min
Deadlocks, starvation, livelock and contention0 of 15 lessons done
- DeadlockIntermediate≈ 20 min
- The Four ConditionsIntermediate≈ 15 min
- Preventing DeadlockIntermediate≈ 15 min
- Lock OrderingIntermediate≈ 20 min
- LivelockIntermediate≈ 15 min
- StarvationIntermediate≈ 15 min
- FairnessIntermediate≈ 15 min
- Priority InversionIntermediate≈ 15 min
- What Contention Actually CostsIntermediate≈ 20 min
- Lock ConvoysIntermediate≈ 15 min
- False ParallelismIntermediate≈ 15 min
- OversubscriptionIntermediate≈ 20 min
- The Cost of a Context SwitchIntermediate≈ 15 min
- More Threads Is Not More SpeedIntermediate≈ 15 min
- Busy WaitingIntermediate≈ 20 min
Atomics, memory ordering and lock-free0 of 15 lessons done
- Atomics: What Is Actually IndivisibleAdvanced≈ 20 min
- Compare-and-Swap and the Retry LoopAdvanced≈ 15 min
- Atomics Are Not MagicAdvanced≈ 15 min
- Lock-Free Is a Progress GuaranteeAdvanced≈ 15 min
- A Lock-Free Stack, and What the Teaching Version OmitsAdvanced≈ 20 min
- Wait-Free vs Lock-Free: Whose Progress Is GuaranteedAdvanced≈ 15 min
- The ABA Problem: The Value Came BackAdvanced≈ 15 min
- What a Memory Model DefinesAdvanced≈ 20 min
- Happens-Before: The Edge That Makes a Write VisibleAdvanced≈ 15 min
- Reordering: The Compiler and the CPU Both Do ItAdvanced≈ 15 min
- Memory Barriers Constrain Ordering, Not CachesAdvanced≈ 15 min
- False Sharing: Different Variables, Same Cache LineAdvanced≈ 15 min
- What a Shared Write CostsAdvanced≈ 15 min
- Safe Publication: Handing Over a Finished ObjectAdvanced≈ 20 min
- Double-Checked Locking: The Canonical Cautionary TaleAdvanced≈ 20 min
Parallel algorithms, fork/join and SIMD0 of 18 lessons done
- Fork/JoinAdvanced≈ 20 min
- Parallel ReduceAdvanced≈ 20 min
- The Map/Reduce PatternAdvanced≈ 20 min
- Parallel AlgorithmsAdvanced≈ 20 min
- Parallel OverheadAdvanced≈ 15 min
- Amdahl's LawAdvanced≈ 20 min
- Gustafson's LawAdvanced≈ 20 min
- Work and SpanAdvanced≈ 20 min
- Dependency GraphsAdvanced≈ 20 min
- SIMD: One Instruction, Many ElementsAdvanced≈ 15 min
- Task Parallelism vs Data ParallelismAdvanced≈ 15 min
- GPU Parallelism: Thousands of Lanes, One BusAdvanced≈ 15 min
- Pipeline Parallelism: Different Items, Different StagesAdvanced≈ 20 min
- Fan-Out / Fan-In: One Request Becomes NAdvanced≈ 15 min
- Scatter/Gather and the Tail You InheritAdvanced≈ 20 min
- BarriersAdvanced≈ 20 min
- Latches & CountdownsAdvanced≈ 20 min
- Parallelism Moves the Load DownstreamAdvanced≈ 20 min
Parallel performance, debugging and production concurrency0 of 31 lessons done
- Why Eight Cores Give You Four and a HalfAdvanced≈ 15 min
- Memory Bandwidth: More Cores, Same BusAdvanced≈ 15 min
- Parallelism Can Destroy LocalityAdvanced≈ 15 min
- NUMA: Not All Memory Costs the SameAdvanced≈ 15 min
- Thread Affinity: Pinning, and What It Costs YouAdvanced≈ 15 min
- Reduction Ordering: The Sum Changed When the Worker Count DidAdvanced≈ 15 min
- Determinism: Same Input, Same Output?Advanced≈ 15 min
- Ordering Guarantees: Four Levels, Four PricesAdvanced≈ 15 min
- What to Instrument in a Concurrent SystemAdvanced≈ 10 min
- Hold Time, Wait Time, and the Ratio Between ThemAdvanced≈ 10 min
- Reading a Thread DumpAdvanced≈ 15 min
- Task Dumps: When the Threads Look Idle and Nothing Is MovingAdvanced≈ 10 min
- Off-CPU Time: The Thing a CPU Profiler Cannot SeeAdvanced≈ 10 min
- Race Detectors: What They Find, and What They Structurally CannotAdvanced≈ 15 min
- Heisenbugs: The Bug That Leaves When You Look at ItAdvanced≈ 10 min
- Deterministic Replay: Making the Schedule ReproducibleAdvanced≈ 15 min
- Stress Testing: A Test That Passed Once Proves NothingAdvanced≈ 15 min
- Choosing a Concurrency Model for a ServerAdvanced≈ 15 min
- Thread per Request: The Model That Reads Like Ordinary CodeAdvanced≈ 15 min
- Event-Driven Servers: Many Connections, One LoopAdvanced≈ 10 min
- Hybrid Runtimes: It Was Never Threads Versus AsyncAdvanced≈ 10 min
- UI Concurrency: One Thread Owns the ScreenAdvanced≈ 15 min
- The Database Solves Concurrency For Its Data, Not For Your MemoryAdvanced≈ 15 min
- What Changes When the Shared State Is on Another MachineAdvanced≈ 15 min
- A Mutex on Server A Does Nothing About Server BAdvanced≈ 15 min
- The Concurrency Pattern CatalogueAdvanced≈ 20 min
- Concurrency Anti-PatternsAdvanced≈ 20 min
- Can These Tool Calls Run At the Same Time?Advanced≈ 10 min
- Parallel Tool Calls: Lower Latency, and Four BillsAdvanced≈ 15 min
- Two Agents, One DocumentAdvanced≈ 15 min
- Cancelling an Agent Run: What Actually StopsAdvanced≈ 15 min
What work are you trying to run?
The execution model is derived from the work, not chosen from a catalogue. Pick the work and the first question is about the work — never about a primitive.
Is every core already busy, or is one core busy and seven idle — and do you know which?
- 1Classifying the Work: Computing or Waiting?Before you can choose an execution model you have to know what the work is doing with the wall clock. Compression, resizing, encryption, parsing and numerics compute. Network calls, database queries, disk reads and third-party APIs wait. Most real handlers do both, in phases, and the phases want different models.
- 2Why Parallelism Exists: ComputeOne billion floats to sum. One core does it in 400 ms. Four cores should do it in 100 ms and will actually do it in about 130. Parallelism exists because a single core stopped getting faster around 2005 and the only remaining lever was more of them.
- 3Choosing an Execution ModelSeven questions — bound, sharing, independence, ordering, task count, duration, isolation — and seven answers: sequential, threads, processes, async, worker pool, message passing, parallel algorithm. Every answer comes with why it wins, what it costs and how it fails, because a recommendation without those three is a preference.
- 4Fork/JoinSplit the work, run the pieces, wait for all of them, combine. The split is easy and the combine is arithmetic — the join is the part that carries the correctness, because it is the only place a happens-before edge exists between the children and the parent that reads their results.
- 5Parallel OverheadSplitting, scheduling, synchronizing and combining are work the sequential version never does. For small tasks that overhead exceeds the task, and the parallel version is measurably slower on more hardware — which is why every parallel decomposition needs a size below which it stops decomposing.
- 6Amdahl's LawThe part that cannot be parallelised sets a ceiling on everything else. If a tenth of the job must happen in sequence, an infinite number of cores still cannot make it more than ten times faster — and long before infinity, each extra core is buying almost nothing.
The learning loop
Every lesson answers this chain as explicit fields — including the two that are never optional here: what invariant must hold under every interleaving, and what complexity the concurrency cost you.
Flagship experiences
Step a schedule one indivisible operation at a time and watch an invariant hold, then die. The trace is the evidence; the claim underneath it is what you take away.
You drive the scheduler. Interleave two actors' operations yourself and try to break the stated invariant — then see what each fix costs.
Acquire locks in whatever order you like, watch the wait-for graph grow an edge at a time, and find the cycle. Then break one of the four conditions and see which ones are actually available to you.
Move the serial fraction and the per-task overhead and watch the speedup curve bend away from linear. Amdahl and Gustafson answering different questions about the same program.
Every concurrent design is bought with complexity. Price one: what is shared, what must be synchronized, what can deadlock, and what the speedup was actually worth.
Convoys, oversubscription, priority inversion, a starved writer, a stalled event loop. Predict which one the symptom describes before the schedule is revealed.
Take a dependency graph, find the independent work, and read the ceiling off work versus span — including the cases where the honest answer is that it does not parallelise.
One system, end to end: classify the work, choose the execution model, name the invariants, defend the synchronization, then survive the schedules injected against your own design.
Learning modules
Should this work overlap, run simultaneously, or neither?
The distinction the whole domain rests on: overlapping progress versus simultaneous execution, why each exists, how to classify work as waiting-bound or compute-bound, and the honest admission that concurrency is always bought with complexity.
What unit actually carries this work, and who schedules it?
Processes, threads, tasks and coroutines as distinct things a runtime can hand you — and the language-specific reality of what each one buys in C++, JavaScript and CPython. The kernel-side mechanics live in Operating Systems.
What does suspending a task actually do, and what still runs?
Async/await as an execution model rather than syntax: futures and promises, the event loop, worker threads and web workers, and the two lessons that matter most — async is not parallelism, and a CPU-bound handler stalls the loop for everyone.
What is shared, what must stay true, and which schedule breaks it?
The core of the domain. Shared mutable state, invariants, finding the minimal critical section, enumerating interleavings to locate a failing schedule, and the distinction between a race condition and a data race that most engineers conflate.
Which guarantee does this primitive give, and which does it not?
Mutexes and the scope you put them around, read/write locks, semaphores as permit counters, condition variables and the predicate loop — with lost and spurious wakeups as the failures that follow from getting the loop wrong.
Can we avoid sharing state by moving data instead?
Producer/consumer as the flagship pattern, bounded versus unbounded queues, backpressure as the conversation between a fast producer and a slow consumer, channels, message passing and the actor model — coordination without shared memory.
How does a correct-looking system stop making progress?
The four conditions for deadlock and how to break each one, lock ordering as the practical answer, wait-for graphs and their cycles, livelock where everyone is busy and nothing advances, starvation, fairness and priority inversion.
Why did adding threads make it slower?
Eight cores all waiting on one lock. Convoys, false parallelism, oversubscription, the real cost of a context switch, and busy waiting — the module that explains why parallel hardware does not imply parallel throughput.
What does an atomic operation actually make indivisible?
Atomic reads, writes and compare-and-swap, why an atomic variable does not make a multi-step invariant safe, and lock-free concepts treated honestly as progress guarantees rather than performance promises — CAS loops, the ABA problem, wait-free versus lock-free.
Will the other thread even see what I wrote?
The advanced core: threads do not necessarily observe memory operations in source order. Happens-before, compiler and CPU reordering, barriers, false sharing, cache coherence traffic, safe publication and why double-checked locking is the canonical cautionary tale.
How many workers, and what happens to the work that does not fit?
Pools as bounded resource use rather than unlimited spawning, the reasoning behind sizing (and the refusal to give a universal formula), work stealing, saturation, and bounding concurrency as a first-class design decision.
Which parts of this computation are independent, and what limits the speedup?
Fork/join, parallel reduce, map/reduce as a computational pattern, the overhead that makes parallelising small work slower, Amdahl and Gustafson as complementary intuitions, and work versus span as the real ceiling on a dependency graph.
Is the same operation applied to many items, or different operations at once?
SIMD and data parallelism against task parallelism, pipeline stages that let items occupy different stages simultaneously, fan-out/fan-in, scatter/gather and the tail-latency cost of waiting for the slowest branch.
How do independent tasks agree on when to proceed?
Barriers and latches, `Promise.all` and `gather` and what they do on failure, the sequential-await trap, and the counterweight: parallelising a call fan-out multiplies load on whatever is downstream. Thundering herds and single-flight coalescing.
Who owns this task, and what happens when nobody wants the answer?
Child tasks with a clear lifetime, cancellation as a first-class concern rather than an afterthought, propagation to children, timeouts against deadlines, and the orphaned background task nobody is waiting for and nobody stops.
Can we make coordination unnecessary instead of correct?
The strategies that reduce synchronization rather than perfecting it: immutability, copying versus sharing, copy-on-write snapshots, and optimistic against pessimistic concurrency control with the contention threshold that decides between them.
What shape does this problem already have a known answer for?
The pattern catalogue with problem, structure, trade-offs and failure modes for each — and the anti-patterns that show up in real code review: one global lock, unbounded spawning, locks held across I/O, swallowed task exceptions, and lock-free code written without a reason.
Why is eight cores not eight times faster?
Where the speedup goes: serial fractions, synchronization, memory bandwidth, cache locality destroyed by tasks bouncing between cores, NUMA, affinity, and the numerical fact that parallel reduction can change floating-point results.
The bug disappears when I add a log line. Now what?
Why concurrency bugs resist ordinary debugging, and what works anyway: lock wait metrics, thread and async task dumps, contention profiling, race detectors, deterministic replay, and stress testing designed to shake out schedules that normal runs never produce.
Which concurrency model should this server use?
Thread-per-request against event-driven against hybrid runtimes, UI thread constraints, how databases solve concurrency for data but not for your application memory, and the boundary where a local mutex stops meaning anything at all.
Can these tool calls run at the same time?
Parallel tool execution and what it costs in load, money and determinism; agents racing on the same document; and cancelling a long-running agent run without leaving tool calls, queue entries and side effects behind.
What this domain is not
It is not a list of thread APIs, and it does not re-teach the mechanisms.
Operating Systems teaches you what a mutex is. This domain tells you which region to put it around, what happens in the schedules where you got that wrong, and whether you needed one at all. Where a lesson touches a mechanism — a thread, a semaphore, a context switch, an event loop — it links the Operating Systems lesson and moves straight on to the reasoning. Nothing here asks you to memorize an API surface; every lesson asks what invariant is being protected, which interleavings exist, what the synchronization actually guarantees, and what the concurrency cost.
Concurrency is overlapping progress — a structure in which several pieces of work are all underway. Parallelism is simultaneous execution — more than one thing running at the same instant on different hardware. A system can be concurrent without being parallel, parallel without being concurrent, both, or neither. Almost every confused conversation about performance starts here.
A race condition is a logical bug: correctness depends on timing, and some schedule produces a wrong answer. A data race is a memory-model term: unsynchronized conflicting access to the same location, at least one of them a write. C++ makes a data race undefined behaviour; JavaScript and Python do not have the same rule. You can have a race condition with no data race anywhere — that is the common case in production.
A runtime may schedule thousands of tasks over a handful of threads, or one task per thread, or move a task between threads mid-flight. "Ten thousand concurrent requests" says nothing about how many threads exist. Ask what the unit is, and who schedules it.
The guarantees you program against come from the language: what is atomic, what establishes happens-before, what the compiler may reorder. Underneath, the processor has its own ordering rules. They are related, they are not the same, and reasoning at the wrong level is how "but I wrote it in that order" bugs get shipped.
Every lesson carries required complexity and alternatives fields. A lesson that cannot name the simpler thing to try first is teaching a default rather than a decision — and concurrency is always bought with complexity, so the price goes on the label.
Every schedule, timeline and speedup curve on this site is produced by a model here and is labelled as such. There is no universal ideal thread count, and lock-free is a progress guarantee, not a performance promise.