Operating systems / Race conditions

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.

Intermediate7 steps~6 min
Thread A — adds 1 to the counter
Thread B — adds 1 to the same counter
Counter — one number in memory both can reach
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 Two threads, one counter
THE MATH
counter = 5 A: counter = counter + 1 B: counter = counter + 1 expected: 7
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
VALUES SO FAR
counter5shared
expected7goal
one line····3 steps
A then B····correct
mixed····wrong
lock····always
atomic····always

Spotted a mistake on this page?