C++ Reference ยท Types and Language Concepts
The C++ memory model
Concept page on the C++ memory model: data races, atomics, happens-before, memory_order acquire and release, sequential consistency and fences.
The memory model is the part of the language that says what a thread may observe when other threads read and write the same memory. Since C++11 the standard defines this precisely, so portable multithreaded code does not depend on a particular CPU. Two compilers and two processors may reorder operations, cache values in registers, and delay writes; the model tells you which of those transformations are visible.
The central rule: if two threads access the same memory location, at least one access is a write, and the accesses are not ordered by happens-before and are not both atomic, that is a data race, and a data race is undefined behavior. To avoid it you use mutexes, atomics, or other synchronization primitives such as std::thread::join, futures and latches.
std::atomic<int> hits{0}; hits.fetch_add(1); // safe from many threads
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Key ideas
Happens-before
Within one thread, statements are sequenced-before each other. Between threads, an operation A synchronizes with B when, for example, A is a release store that B's acquire load reads, when a thread starts (the std::thread constructor call happens before the new thread's first action), when join() returns, or when one thread unlocks a mutex and another then locks it. Chaining these gives happens-before: if A happens before B, B sees A's effects.
Atomics and memory orders
Operations on std::atomic<T> are indivisible and never race. Each takes an optional std::memory_order:
| Order | Guarantee |
|---|---|
memory_order_relaxed |
Atomicity only. No ordering with other memory. Fine for counters and statistics. |
memory_order_acquire |
On a load: later reads and writes in this thread cannot move before it. Pairs with a release. |
memory_order_release |
On a store: earlier reads and writes cannot move after it. Pairs with an acquire. |
memory_order_acq_rel |
Both, for read-modify-write operations. |
memory_order_seq_cst |
The default. Acquire/release plus a single total order of all seq_cst operations that every thread agrees on. |
memory_order_consume |
Discouraged; implementations treat it as acquire. |
The default of seq_cst is the easiest to reason about and is the right choice unless you have measured a problem.
Fences and other tools
std::atomic_thread_fence orders surrounding operations without an atomic variable of its own. std::atomic_flag is a minimal always-lock-free flag (with wait/notify since C++20), and std::atomic_ref<T> (C++20) applies atomic operations to an existing object. Condition variables, latches, barriers and semaphores provide higher-level synchronization.
Usage notes
volatileis not for threads. It does not make operations atomic or ordered; usestd::atomic.- Tearing and visibility: even a plain
boolflag read in a loop may be hoisted into a register by the optimizer, so the loop never ends. Make itstd::atomic<bool>. - Non-atomic
intincrements race because++xis read, add, write. Usefetch_addor a mutex. - Compare-exchange loops:
compare_exchange_weakmay fail spuriously and belongs in a loop;compare_exchange_strongdoes not fail spuriously. - Lock-free is not guaranteed for every type. Check
std::atomic<T>::is_always_lock_freeoris_lock_free(). - Mutexes are fast enough for most code. Reach for relaxed and acquire/release orders only after measuring and after you can prove the algorithm correct.
- Tools: compile with
-fsanitize=threadto find races. Output of racy programs differs between runs, so tests that pass prove little. - Initialization of function-local statics is thread-safe since C++11, so a Meyers singleton needs no extra locking.