Module 03 · How Computers Work · Many programs, one machine · about 21 min to read
Continue where you left off ×
Concurrency & the Hardware
Concurrency and the Hardware · Open board
Two cores reaching for the same shared counter at once, and the three fixes for it: a lock, an atomic add, and giving each core its own copy.
Before you start. Processes, Threads & the Kernel gave you a thread, one flow of instructions moving through a program, and a process, the box that can hold several threads sharing the same memory. This lesson is about what happens the instant two of those threads reach into that shared memory at once, using the CPU’s one-instruction-at-a-time steps from The CPU (fetch-decode-execute, the ISA, assembly) .
The idea in 30 seconds
Concurrency is how a job is structured so many tasks can be in flight together. Parallelism is more than one core working at the same instant, and you cannot have parallelism with only one core.
counter = counter + 1 is really three steps: read, add, write. Two cores can interleave between those steps and silently lose an update. That is a race condition, the bug that overdosed real patients on a hospital machine in the 1980s.
A lock stops the race by letting one core touch the shared value at a time, correctly, at the cost of every other core waiting its turn.
An atomic add fixes the same race in one uninterruptible hardware step with no waiting line, but every core still pays to catch up with whoever wrote last.
Not sharing at all beats both fixes: give each core its own counter and add the totals at the end.
More cores never buy proportional speed forever. Whatever fraction of a job cannot be split sets a hard ceiling on how much any number of cores can help.
Read the story
Lesson 10 of 41 in How Computers Work
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 21 March 1986: a keystroke, and a beam with no target A kitchen with one cook, then two: concurrency and parallelism One pencil at a time: the lock, and its bill A pencil stroke nothing can interrupt: the atomic fix Ten helpers, one step nobody can split Measured on this machine Deadlock, and its cousins Cache coherence and false sharing Four ways to survive sharing Amdahl’s Law, worked and measured Terms it explains: race condition, concurrent, parallelism, lock, atomic, lost update, Compare-and-swap (CAS), lock-free, deadlock, 2PL, SSI, Cache-coherence, false sharing, happens-before, linearizability, Threads and locks, Actors, async / event loop, CSP / channels, actor / message passing, Amdahl's law, cache coherence (MESI), CAS, concurrency, coroutine/channel, critical section, memory barrier/fence, memory reordering, mutex/lock, mutual exclusion, starvation / livelock.
Review How Computers Work
Mark as read Saved in this browser, and to your account when you sign in.