As the lead of a C++ codebase, when do you allow hand-written lock-free code with explicit memory orders, and what conditions do you attach?
answer
- measured need, not taste
- seq_cst default, weaker orders justified
- confine it to small primitives
- ThreadSanitizer plus weak-memory hardware
- who can review it
basics
~20 sAllow it only where a measured latency or blocking requirement rules out a mutex, confined to a small reviewed primitive. Require the seq_cst default unless a comment justifies each weaker order, ThreadSanitizer runs, testing on a weakly ordered CPU and a named reviewer.
solid answer
~50 sStart from the fact that a `std::mutex` is correct by construction and usually fast enough; lock-free code with relaxed or acquire/release orders is correct only if every ordering argument is right, and its bugs are data races — undefined behavior — that may pass every test on x86-64. So the bar is a **measured** need: a real-time thread that must never block, a profiled hot path where the lock dominates, or a signal handler. Then confine the code to a small primitive (an SPSC ring, a counter, a flag) behind a narrow interface; prefer a vetted library over a new design; keep `seq_cst` unless a comment states the happens-before argument for each weaker order; `static_assert(is_always_lock_free)` where blocking matters; run ThreadSanitizer in CI and stress tests on arm64; and require a reviewer who can check the ordering. Revisit when the measurement changes.
go deeper
Know that lock-free code is a team decision and that a mutex is the safe default for shared data.
Explain why a wrong memory order is a data race on the surrounding plain data and why tests on x86-64 may not reveal it.
Name the situations where blocking is unacceptable, and the evidence — sanitizer runs, arm64 stress tests, ordering comments — that a lock-free primitive needs.
Write the rule: where lock-free code may live, which orders need justification, who reviews it, and what measurement retires it.
## Why this is a decision, not a technique C++ gives every team the tools to write lock-free code: `std::atomic`, six memory orders, fences, `compare_exchange_weak`. What it does not give is any check that the ordering is right. A wrong memory order is a **data race** on the surrounding plain data, and a data race is **undefined behavior** — the program may pass every test on x86-64, whose hardware hides most reordering, and fail on arm64 or after a compiler upgrade. A lead's question is therefore not "can we write it?" but "who will prove it correct now, and who will keep it correct when someone edits it in two years?" ## The default answer For most code the answer is **a mutex**: - `std::mutex` with `std::scoped_lock` is correct by construction and readable by every reviewer. - An uncontended lock on mainstream platforms is cheap, and a heavily contended atomic still serialises every thread on one cache line. - A lock-free structure rarely beats a well-sized lock until contention is high, and then the design (sharding, batching) matters more than the primitive. ## When lock-free earns its place | Situation | Why a mutex fails | Typical primitive | |---|---|---| | Real-time audio or control thread | it must never block on a lower-priority thread | SPSC ring with acquire/release | | Signal or interrupt handler | the interrupted code may hold the lock | lock-free `std::atomic` flag or counter | | Profiled hot path where the lock is the top cost | measured, not guessed | relaxed statistics counter, sharded counters | | Library you own whose users demand it | contract requirement | reviewed queue or allocator | A need that does not fit a row — "it feels faster", "lock-free is modern" — does not justify the risk. ## The conditions to attach 1. **Confinement.** Lock-free code lives in a few small primitives with a narrow, documented interface; application code uses the primitive, never raw memory orders. 2. **Prefer a vetted implementation.** A well-tested library queue beats a new one; a new one needs a written design. 3. **Default ordering.** Every atomic operation uses the default `memory_order::seq_cst` unless a comment beside it states which release it pairs with, or why nothing depends on its order. `memory_order::consume` is not used (C++26 deprecates it). 4. **Lock-freedom asserted.** Where the reason is "must not block", `static_assert(std::atomic<T>::is_always_lock_free)` makes a port to a target that needs a lock fail at build time. 5. **Tooling.** ThreadSanitizer builds in CI for the primitive's tests, stress tests that run long enough to interleave, and a run on a weakly ordered CPU such as arm64 — x86-64 alone hides most ordering bugs. 6. **Reviewers.** At least one reviewer who can state the happens-before argument; if nobody can, the code is not maintainable by the team and the answer is no. 7. **Exit criteria.** Keep the benchmark that justified it, and re-run it; if a mutex version is now within noise, replace the lock-free one. ## The cost a lead weighs - **Review cost** grows with every weak order, and only a few engineers can do it. - **Bug cost**: ordering bugs surface rarely, on other hardware, and often as data corruption rather than a crash. - **Portability**: lock-freedom of a type is a property of the target; `is_always_lock_free` can be true on one platform and false on another. - **Opportunity cost**: time spent on a lock-free queue is not spent on reducing sharing, which usually wins more. ## A workable written rule "Atomics with the default order are allowed anywhere for independent flags and counters. Any weaker memory order, fence or compare-exchange loop lives in the concurrency primitives directory, carries an ordering comment, a TSan-enabled test and an arm64 stress run, and needs approval from a listed reviewer." That keeps the common case easy and the dangerous case deliberate.
- A developer shows a relaxed-ordered queue that passes all tests on x86-64 developer machines. What do you ask for?The written happens-before argument for each operation that is not seq_cst, a ThreadSanitizer run of the tests, and a long stress run on an arm64 machine, because x86-64 keeps stores in order and hides most missing release/acquire pairs. Then a benchmark against a mutex-protected version under realistic contention. Without the argument and the arm64 run, passing tests prove little.
- Would you ban std::memory_order_relaxed outright?No — a blanket ban costs clarity for little safety. Relaxed is easy to justify for independent statistics counters and id generators, where nothing is inferred from the value. Allow it with a comment saying so, and treat relaxed loads or stores that guard other data as the review red flag, since that is where the data races appear.
- Lock-free code is always faster than a mutex, so prefer it by default.
- If the tests pass on x86-64, the memory orders are correct.
- Weaker memory orders are a safe micro-optimisation anyone can apply.
- A lock-free design removes the need for a race detector.
- Any atomic type is lock-free on every target we ship.