Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Concurrency

C++ memory_order and the memory model

Reference for std::memory_order: relaxed, acquire, release, acq_rel and seq_cst, the happens-before relation, and how to publish data between threads safely.

When two threads touch the same data, the compiler and the CPU are allowed to reorder ordinary reads and writes, and one thread may see another thread's writes in a different order than they were issued. The C++ memory model defines which reorderings are permitted. Every std::atomic operation takes a std::memory_order argument that says how much ordering it must provide around itself.

The idea behind the useful orders is release/acquire. A thread writes some ordinary data, then does a release store on an atomic flag. Another thread does an acquire load of the same flag and sees the new value. From then on, everything the first thread wrote before the release is guaranteed visible to the second thread. That "synchronizes-with" edge creates a happens-before relationship, which is what makes ordinary data accesses race-free.

C++
std::atomic<bool> ready{false};
int payload = 0;

// thread A                          // thread B
payload = 42;                        while (!ready.load(std::memory_order_acquire)) {}
ready.store(true, std::memory_order_release);   // payload is 42 here

Try it

C++ Publishing data with release and acquire

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++ Relaxed ordering for plain counters

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.

The six memory orders

C++
enum class memory_order {      // C++20 scoped enum; C++11 constants memory_order_relaxed etc. still exist
    relaxed, consume, acquire, release, acq_rel, seq_cst
};
Order Meaning Valid for
relaxed Only atomicity. No ordering of surrounding memory accesses. load, store, read-modify-write
acquire Nothing after this load may be moved before it. Pairs with a release. load, read-modify-write
release Nothing before this store may be moved after it. Publishes earlier writes. store, read-modify-write
acq_rel Both acquire and release. read-modify-write
seq_cst Acquire/release plus a single global order of all seq_cst operations. The default. everything
consume Dependency-ordered load. Compilers treat it as acquire; its use is discouraged. load

Errors / undefined behavior: load must not use release or acq_rel; store must not use acquire or acq_rel. The failure order of compare_exchange cannot be release or acq_rel (and, before C++17, cannot be stronger than the success order).

Usage notes

  • Start with the default seq_cst. It is the easiest to reason about. Weaken it only after profiling shows it matters, and only with a clear argument in a comment.
  • Acquire and release only mean something in pairs on the same atomic variable, with the acquire actually reading the value written by the release (or a later value in its release sequence).
  • Relaxed is for independent counters and statistics, where no other data depends on the value you read.
  • Thread creation and join() already synchronize. Data written before starting a thread is visible inside it, and data written by a thread is visible after join(); mutexes, futures and latches also provide acquire/release edges.
  • Fences: std::atomic_thread_fence(order) provides ordering without an atomic operation of its own; prefer ordering on the operations.
  • x86 hides bugs. Its hardware already gives acquire/release for ordinary loads and stores, so code with too-weak orders often works on x86 and breaks on ARM. The compiler may still reorder.
  • A data race on non-atomic data is undefined behavior whatever orders you use elsewhere. The orders only help if they create happens-before between the conflicting accesses.
  • Never use volatile for synchronization. It is not atomic and gives no ordering between threads.