Developer Tools
Explaining acquire-release ordering with code
Acquire-release ordering is easier to teach through one producer and one consumer than through definitions. This guide walks through a worked example and three decisions that make the explanation dependable.
01
Step one: pick a single published value
Start with one thread preparing data and another reading it. In C++, the producer writes a plain integer called payload, then stores true into an atomic flag called ready using release ordering. The consumer loops until it loads ready with acquire ordering, then reads payload. State the promise plainly: if the consumer observes true, every write the producer made before the release store is visible afterward. Keep the example tiny so the reader sees only one relationship, publication, rather than a crowd of unrelated atomics competing for attention.
02
Step two: show the broken relaxed version
Next, change both operations to relaxed ordering and explain what is no longer promised. The flag may still flip, yet the consumer is not guaranteed to see the payload write, because nothing links the two threads' histories. Avoid claiming it will fail on every machine; say the program loses its guarantee and may behave differently across compilers and processors. Pair the broken and fixed versions side by side, highlighting the single changed keyword. This contrast teaches more than a definition, because readers see precisely which argument carries the safety.
03
Step three: decide how readers will verify it
Finish by choosing how the audience can check understanding. Offer a short exercise: add a second payload field and ask which operation must come first. Recommend testing under a thread sanitizer or model checker, while noting that passing runs do not prove correctness. Suggest labeling each atomic operation in comments with what it publishes or acquires. Readers then leave with a repeatable habit: name the data being handed over, mark the release and acquire pair, and review that pair whenever the code changes.