Memory order
Memory order is the constraint you attach to an atomic operation that says how it synchronises with other atomics and with ordinary reads and writes. seq_cst is the default and the safest. acquire/release pair a publish with a consume. relaxed only makes the one access atomic.
Detailed Explanation
The compiler and the CPU reorder independent operations. In a single thread you cannot tell. Across threads, a write to data and a write to flag can become visible in the other order unless you create a happens-before edge. Memory orders on atomics are how you create those edges.
memory_order_relaxed. The operation is atomic (no torn read) and nothing else. Two relaxed operations on different variables have no ordering. Use it for hit counters that do not publish other data.
memory_order_release (store) and memory_order_acquire (load). If a release store in thread A is read by an acquire load in thread B, then B sees all writes that happened in A before the store. That is the publish protocol: write the payload, then release-store a flag or pointer; the other thread acquire-loads the flag, then reads the payload.
memory_order_acq_rel. For read-modify-write: acquire on the load side, release on the store side.
memory_order_seq_cst. All seq_cst operations in the program sit in one total order that every thread agrees on. It is a superset of acquire/release. It is the default. It is what you should use until a profiler and a second reader agree you need less.
memory_order_consume. Designed for data-dependent loads. Compilers treat it as acquire. Do not use it.
volatile does not substitute for any of these. A mutex's lock is an acquire and unlock is a release; if you use mutexes you are already ordered.
Wrong orders look like "it works on x86". x86 is a strong hardware model; many illegal C++ programs still appear to work there. ARM and POWER will show the bug. ThreadSanitizer and a formal reading of happens-before are the checks.
If you cannot draw the acquire/release pair on a whiteboard, use seq_cst or a mutex. Clever memory orders are a liability in a codebase that more than one person will edit.
Code Examples
#include <atomic>
#include <cassert>
#include <thread>
int payload = 0;
std::atomic<bool> ready{false};
void producer() {
payload = 42; // ordinary write
ready.store(true, std::memory_order_release); // publish
}
void consumer() {
while (!ready.load(std::memory_order_acquire)) {
}
assert(payload == 42); // sees the write
}
int main() {
std::thread a(producer);
std::thread b(consumer);
a.join();
b.join();
}#include <atomic>
std::atomic<int> hits{0};
void bump() {
hits.fetch_add(1, std::memory_order_relaxed); // counter only
}Common Gotchas
Relaxed store of a flag that publishes a payload
the consumer can see `flag == true` and a stale payload. Use release/acquire or seq_cst.
It works on your laptop
x86 hides missing fences. Test on ARM or with TSan.
Mutexes already order
adding atomics inside a locked region for "speed" is usually wasted complexity.
`consume` is not portable in practice
write `acquire`.