Skip to content
Tutorials Center
My learning

Topics

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.

C++
std::atomic<int> hits{0};
hits.fetch_add(1);          // safe from many threads

Try it

C++ Atomic counter and mutex-guarded counter

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.

C++ Release and acquire publish data

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

  • volatile is not for threads. It does not make operations atomic or ordered; use std::atomic.
  • Tearing and visibility: even a plain bool flag read in a loop may be hoisted into a register by the optimizer, so the loop never ends. Make it std::atomic<bool>.
  • Non-atomic int increments race because ++x is read, add, write. Use fetch_add or a mutex.
  • Compare-exchange loops: compare_exchange_weak may fail spuriously and belongs in a loop; compare_exchange_strong does not fail spuriously.
  • Lock-free is not guaranteed for every type. Check std::atomic<T>::is_always_lock_free or is_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=thread to 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.