Skip to content

Shared memory

The same bug, with a lock in the room

A check and an action that are each protected, and the gap between them that is not.

12 minutes · atomicity · critical section boundaries · compound actions

Locking each half is not locking the whole

Once people learn about locks, the next mistake is locking the individual operations rather than the sequence that has to be indivisible. Reading the balance safely and writing the balance safely does not make checking-then-withdrawing safe. The gap between the two is exactly as wide as it was before.

This is the same shape as the cache stampede in the async track. It is worth noticing that it is the same shape, because it means the fix is the same idea: make the check and the act into one thing.

Everything you do here stays in this browser.