Memory barriers
advanced · occasionally asked
The same program with a fence between the write and the read. The outcome that no ordering could explain becomes unreachable again.
Enumerating the interleavings…
What this shows
A barrier does not make anything atomic. It forces publication, which is a different guarantee and the one the reordering was violating.
Why it is asked
The mechanism under every synchronisation primitive you use, and the reason a correct-looking double-checked lock was broken for a decade.
In an interview
The mechanism under every synchronisation primitive you use, and the reason a correct-looking double-checked lock was broken for a decade.
- fences
- happens-before
- volatile
- reordering
Run these next
- Visibility and store buffersA write is not visible when it happens, it is visible when it is published. This is where the interleaving model runs out and a memory model is required.
- MutexesA lock removes orderings from the space. The critical section must span the whole read-modify-write — guarding only the write leaves the window exactly where it was.
- Atomic operationsAtomicity is not speed and not a lock. It is indivisibility: no interleaving can slot between the read and the write, so the orderings that lost updates cease to exist.