08

Chapter 08 — One-Slot Hand-off Under Three Orderings

Atomicity and ordering are two separate guarantees; seq_cst, acquire/release, and no ordering at all produce always-correct, correct-by-one-happens-before-edge, and broken outcomes.

Demo 08Source on GitHub ↗

The full source for this example lives in examples/concurrency/08-atomics-and-the-memory-model/ — clone the repo, cd in, and ./demo.sh.

Chapter reference: Doc 8 — Atomics and the Memory Model

What it shows

One producer and one consumer move values through a single-slot hand-off under three synchronization modes: full sequential consistency, the chapter’s minimal acquire/release pairing, and a broken mode using plain, non-atomic counters. Both correct modes must complete with the exact expected sum, every trial — a hard assertion, since the chapter is explicit that both orderings are sufficient. The broken mode is checked, also as a hard assertion, to actually misbehave: with no atomic and no volatile, the optimizer is free to hoist the consumer’s read out of its spin loop, and the demo reliably stalls rather than relying on a classic, less reliable scheduling race. A soft timing check reports — without asserting a fixed percentage — that seq_cst pays measurably more than acquire/release for its stronger guarantee.

Running it

cd examples/concurrency/08-atomics-and-the-memory-model
./demo.sh            # build + run ring_handoff (all three modes) + verify.lua
./demo.sh --clean    # remove the image