Skip to main content
PRISM

The lost update

foundational · asked in almost every interview

Two threads, one shared counter, one `count++` each. Every interleaving of load, add and store, enumerated — and the eighteen of twenty that lose an update marked.

Enumerating the interleavings

What this shows

`count++` is three operations, not one. Only two of its twenty interleavings produce 2; the failure is the common case, and an atomic increment removes the orderings rather than making them rarer.

Why it is asked

Being able to draw the interleaving rather than name it. "Race condition" is recall; showing which two orderings are safe is understanding.

In an interview

Being able to draw the interleaving rather than name it. "Race condition" is recall; showing which two orderings are safe is understanding.

  • race condition
  • read-modify-write
  • atomicity
  • interleaving

Run these next

The rest of races and atomicity