C++ Memory Model and Atomics Basics¶
1. Introduction¶
Concurrent C++ correctness cannot be established by “it works on my machine.” The C++ memory model defines when reads are allowed to observe writes, which reorderings are legal, and when behavior is simply undefined.
The single most important rule is:
A data race is undefined behavior.
This chapter focuses on engineering-safe foundations:
- how to reason with happens-before,
- what each memory order actually promises,
- which low-level patterns are valid and why.
2. Scope and Non-Goals¶
2.1 Scope¶
We cover:
- data race definition and consequences,
- atomic operations and ordering semantics,
- release/acquire publication patterns,
- read-modify-write operations,
- practical design and review checklists.
2.2 Non-Goals¶
We do not attempt to build complete lock-free data structures in this chapter. The goal is correct foundations, not advanced algorithm catalog.
3. Core Vocabulary¶
3.1 Threads of Execution¶
Each thread has its own evaluation order (“sequenced-before” relation locally). Cross-thread visibility requires explicit synchronization.
3.2 Data Race¶
A data race exists if:
- two conflicting operations access the same memory location,
- at least one is a write,
- they are in different threads,
- and they are not ordered by synchronization/happens-before.
If a data race exists, program behavior is undefined.
3.3 Happens-Before¶
A happens-before B means effects of A are visible to B under language rules.
This is the central proof tool for concurrent correctness.
3.4 Modification Order¶
For each atomic object, all writes to that object form a single total order. This order is per-atomic-object, not global across all atomics.
4. Atomic Types: What They Guarantee¶
std::atomic<T> provides atomic operations for supported T.
Atomicity means operations on that object are indivisible with respect to other threads. Atomicity alone is not enough for visibility ordering of non-atomic data.
Example:
This guarantees atomic increment of counter.
It does not automatically order unrelated memory accesses.
5. Memory Orders at a Glance¶
5.1 memory_order_relaxed¶
- atomicity only,
- no synchronization with other operations,
- useful for statistics/counters where ordering is irrelevant.
5.2 memory_order_release¶
- used on stores (or RMW with release semantics),
- ensures prior writes in same thread become visible to a matching acquire observer.
5.3 memory_order_acquire¶
- used on loads (or RMW with acquire semantics),
- prevents subsequent reads/writes from moving before the acquire,
- synchronizes with a corresponding release on same atomic object.
5.4 memory_order_acq_rel¶
- used on read-modify-write operations,
- combines both acquire and release properties.
5.5 memory_order_seq_cst¶
- strongest commonly used order,
- establishes a single global total order for sequentially consistent atomics,
- easiest to reason about, often slower on some architectures.
6. Publication Pattern (Correct Baseline)¶
#include <atomic>
std::atomic<bool> ready{false};
int payload = 0;
void producer() {
payload = 42;
ready.store(true, std::memory_order_release);
}
void consumer() {
while (!ready.load(std::memory_order_acquire)) {
}
// payload == 42 is now guaranteed visible
}
Why it works:
payload = 42is sequenced-before release-store in producer.- acquire-load observes the release-store value.
- synchronization edge is established.
- writes before release become visible after acquire.
7. Incorrect Variant: Relaxed Flag for Publication¶
void producer_bad() {
payload = 42;
ready.store(true, std::memory_order_relaxed); // insufficient
}
void consumer_bad() {
while (!ready.load(std::memory_order_relaxed)) {
}
// payload visibility is not guaranteed
}
Even if this appears to work on some hardware, the language does not guarantee visibility ordering here.
8. Read-Modify-Write Operations¶
RMW operations include:
fetch_add,fetch_sub,exchange,compare_exchange_weak/strong.
These operations are atomic with respect to competing operations on same atomic object.
Example:
Use acq_rel when operation both consumes prior synchronization and publishes subsequent state.
9. CAS Loop Pattern¶
std::atomic<int> value{0};
void increment_if_even() {
int expected = value.load(std::memory_order_relaxed);
while ((expected % 2) == 0) {
if (value.compare_exchange_weak(
expected,
expected + 1,
std::memory_order_acq_rel,
std::memory_order_relaxed)) {
break;
}
// expected updated on failure
}
}
Key points:
compare_exchange_weakmay fail spuriously.- failure memory order must be no stronger than success order.
- loop structure must tolerate retries.
10. Fences: Powerful but Easy to Misuse¶
atomic_thread_fence can impose ordering without directly touching a specific atomic object.
However, fence-based designs are harder to prove and review.
Engineering recommendation:
- prefer operation-associated orders (
store(..., release),load(..., acquire)), - use explicit fences only with strong justification and documentation.
11. volatile Is Not Synchronization¶
In C++, volatile is primarily for special memory access semantics (e.g., MMIO contexts).
It does not provide inter-thread synchronization guarantees.
Do not replace atomics/mutexes with volatile for concurrency correctness.
12. Atomicity vs Composite Invariants¶
Atomic operations protect individual memory locations. They do not automatically protect multi-variable invariants.
If consistency requires multiple fields to move together, use:
- a lock,
- or a formally verified lock-free protocol with explicit ordering proof.
13. Architecture Myths¶
“x86 is strong, so this is safe” is not a language proof.
Correctness must be proven in C++ memory-model terms, not by relying on one CPU family’s observed behavior.
Portability failures often appear only after:
- architecture change,
- compiler upgrade,
- optimization level change.
14. Choosing an Initial Memory Order Strategy¶
Practical strategy for teams:
- start with
seq_cstfor correctness clarity, - write tests and invariants,
- relax to acquire/release or relaxed only with proof,
- benchmark after correctness is established.
Premature weakening of memory order is a common source of latent bugs.
15. Testing and Verification Reality¶
Concurrent bugs are schedule-dependent and non-deterministic. Absence of failure is not proof of correctness.
Use layered validation:
- stress tests with randomized scheduling pressure,
- ThreadSanitizer for race detection,
- model-level reasoning in code review,
- assertions for protocol state transitions.
16. Typical Safe Use Cases for Relaxed Order¶
memory_order_relaxed is acceptable when:
- only atomicity matters,
- no thread uses that atomic as publication/synchronization signal,
- stale value observations are acceptable by design.
Common example: telemetry counters.
std::atomic<unsigned long long> events{0};
void record() {
events.fetch_add(1, std::memory_order_relaxed);
}
17. Typical Publication Pattern with Pointer¶
struct Config {
int threshold;
};
std::atomic<Config*> g_cfg{nullptr};
void publish(Config* p) {
g_cfg.store(p, std::memory_order_release);
}
Config* acquire_cfg() {
return g_cfg.load(std::memory_order_acquire);
}
Consumer observing non-null pointer via acquire load also observes prior initialization performed before release store.
18. When to Prefer Mutex Over Atomics¶
Use mutex if:
- multiple fields must remain consistent together,
- protocol complexity exceeds easy local proof,
- maintenance/readability is a priority,
- contention profile does not justify lock-free complexity.
Lock-free is not automatically faster in end-to-end systems.
19. Review Checklist¶
Before merging atomic code, verify:
- Is there a clear happens-before proof?
- Are data-race-free guarantees explicit?
- Are memory orders minimal but sufficient?
- Is
relaxedused only where ordering is irrelevant? - Are architecture assumptions avoided?
- Is fallback to mutex considered and documented?
If proof is unclear, simplify design.
20. Terminology Precision in Documentation¶
Use terms precisely:
- “undefined behavior” for data races,
- “synchronizes-with” for release/acquire edges,
- “happens-before” for visibility guarantee.
Avoid vague wording like “likely visible” in technical docs.
21. Version Notes¶
- C++11 introduced formal memory model and atomics.
- Later standards expanded atomic support and utility APIs.
- Core correctness principles in this chapter remain stable across modern standards.
Always state language mode in reproducible examples.
Example¶
A runnable version of this topic:
22. Conclusion¶
Correct concurrent C++ begins with language-level reasoning, not processor folklore.
Engineering sequence should be:
- define invariants,
- establish happens-before relations,
- choose memory orders that satisfy proof,
- validate with tooling and stress tests,
- optimize only after correctness is locked.
When this discipline is followed, atomics become a precise tool rather than a source of nondeterministic defects.