Skip to content

Shared memory

Doing it without a lock

Compare-and-swap, the retry loop it needs, and the value that came back.

14 minutes · compare-and-swap · retry loops · ABA

One step, and an answer about whether it happened

Compare-and-swap does a read and a write as one indivisible operation, and tells you whether the value it found was the one you expected. If it was not, nothing was written and you get false.

That false is not an error. It is the answer to 'did anything change while I was thinking?', and the whole technique depends on doing something sensible with it — which means looping. A compare-and-swap whose result is ignored is a lock-free algorithm with the safety removed.