Race conditions
Two threads add 1 to the same counter and one of the additions disappears. A step-by-step look at why timing decides the result, and how locks and atomic operations fix it.
In 30 seconds
A race condition is a bug where the result depends on which of two threads happens to get somewhere first.
It arises because a line like counter = counter + 1 is really three steps: read, add, write. If a second thread reads between the first thread’s read and write, both work from the same old value and one update is lost.
The timing has to line up exactly, so the bug shows up only now and then. The cure is to make the steps indivisible: a lock around the critical section, or an atomic operation.
Race conditions, step by step
Simplified: the lock timeline leaves out the add step. Races also occur between processes, and over files or database rows, not only between threads sharing a variable.
- Step 1: Two threads, one counter. Two threads of the same program share one variable, a counter holding 5. Each wants to add 1 to it, for example to count a page view. Two increments should leave 7, whichever thread goes first.
- Step 2: One line, three steps. To the processor, that one line is three separate actions: copy the counter into a private scratch value, add 1 to the copy, write the copy back. Read-modify-write is not atomic, meaning it is not one indivisible step. Another thread can run in the gaps.
- Step 3: A lucky ordering. Time runs downward. If thread A finishes all three steps before thread B starts, B reads the 6 that A wrote and writes 7. This is the ordering the programmer pictured, and most of the time it is the one that happens.
- Step 4: An unlucky ordering. Now B happens to read the counter after A has read it but before A has written. Both hold a copy of 5, both compute 6, both write 6. One increment has vanished without any error message. The result depended on timing: that is a race condition.
- Step 5: Why it comes and goes. The dangerous window lasts a few billionths of a second, so the two threads rarely collide. Whether they do depends on the scheduler, the load and the number of cores. The bug can pass every test, then appear once in a great many runs, and vanish again when a debugger slows things down.
- Step 6: Fix 1: a lock. The read-modify-write is a critical section: code that only one thread may be inside at a time. A lock (or mutex) enforces that. A takes the lock first, so B has to wait at the door until A unlocks. Every ordering now gives 7, at the price of some waiting.
- Step 7: Fix 2: an atomic operation. For something as small as an increment, processors offer an atomic instruction that reads, adds and writes as one step no other thread can split. Languages expose it as atomic types. It needs no waiting, but it only covers single simple operations; a longer critical section still needs a lock.
Try it yourself
Learn this first
Learn next
- Goroutines and channelsLightweight Go functions that run side by side and pass values through channels. Intermediate
- Transaction isolation levelsFour levels that decide what one transaction may see of another’s work. Intermediate
- Dirty reads, phantom reads and anomaliesFour ways two overlapping transactions can show each other wrong data. Intermediate
Spotted a mistake on this page?