Chapter 05 — Uncontended vs. Contended Mutex Cost
An uncontended lock is a single atomic instruction that never leaves user space; a contended one crosses into the kernel, at orders of magnitude more cost.
The full source for this example lives in
examples/concurrency/05-futex-cost/— clone the repo,cdin, and./demo.sh.
Chapter reference: Doc 05 — Shared State and the Futex
What it shows
The same fixed number of counter increments runs three ways: no mutex at all, a std::mutex with no contention, and the identical mutex split across several threads that actually contend for it. Correctness — the final count matching the iteration count exactly — is checked as a hard assertion in all three modes, since contention is a performance concern, never a correctness one. Where strace and ptrace are available, the demo counts futex syscalls directly to show the uncontended lock reaching the kernel about as often as no lock at all, and the contended lock reaching it orders of magnitude more; where tracing is restricted, it falls back to a behavioral timing comparison instead of failing outright.
Running it
cd examples/concurrency/05-futex-cost
./demo.sh # build + run futex_cost (all three modes) + verify.lua
./demo.sh --clean # remove the image