C++11 Memory Model
suggest changeVersions
Remarks
Two conflicting actions on the same memory location participate in a data race when they are potentially concurrent, at least one modifies the location, at least one is not atomic, and neither happens before the other. A data race is undefined behavior. To avoid it, stop those threads from concurrently executing such conflicting operations.
Synchronization primitives (mutex, critical section and the like) can guard such accesses. The Memory Model introduced in C++11 defines two new portable ways to synchronize access to memory in multi-threaded environment: atomic operations and fences.
Atomic Operations
It is now possible to read and write a given memory location with atomic load and atomic store operations. For convenience these are wrapped in the std::atomic<T> template. It holds a value of type T, and loads and stores of that object are atomic.
T must be trivially copyable. That includes integrals, pointers, and types such as std::pair<bool, char>. std::atomic<unsigned> and std::atomic<std::vector<foo>*> are valid. Whether an instantiation is lock-free is a separate question: only std::atomic_flag is guaranteed lock-free. The integral and pointer specializations are the ones implementations usually make lock-free.
Atomic operations have the following properties:
- All atomic operations can be performed concurrently from multiple threads without causing undefined behavior.
- An atomic load will see either the initial value which the atomic object was constructed with, or the value written to it via some atomic store operation.
- Atomic stores to the same atomic object are ordered the same in all threads. If a thread has already seen the value of some atomic store operation, subsequent atomic load operations will see either the same value, or the value stored by subsequent atomic store operation.
- Atomic read-modify-write operations allow atomic load and atomic store to happen without other atomic store in between. For example one can atomically increment a counter from multiple threads, and no increment will be lost regardless of the contention between the threads.
- Atomic operations receive an optional
std::memory_orderparameter which defines what additional properties the operation has regarding other memory locations.
std::memory_order |
Meaning |
|---|---|
std::memory_order_relaxed |
no additional restrictions |
std::memory_order_release / std::memory_order_acquire |
if a load-acquire sees the value stored by a store-release, stores sequenced before the store-release happen before loads sequenced after the load-acquire |
std::memory_order_consume |
like memory_order_acquire, but only for dependent loads |
std::memory_order_acq_rel |
combines load-acquire and store-release |
std::memory_order_seq_cst |
sequential consistency |
These memory order tags allow three different memory ordering disciplines: sequential consistency, relaxed, and release-acquire with its sibling release-consume.
Sequential Consistency
If no memory order is specified for an atomic operation, the order defaults to sequential consistency. This mode can also be explicitly selected by tagging the operation with std::memory_order_seq_cst.
With this order no memory operation can cross the atomic operation. All memory operations sequenced before the atomic operation happen before the atomic operation and the atomic operation happens before all memory operations that are sequenced after it. This mode is probably the easiest one to reason about but it also leads to the greatest penalty to performance. It also prevents all compiler optimizations that might otherwise try to reorder operations past the atomic operation.
Relaxed Ordering
The opposite to sequential consistency is the relaxed memory ordering. It is selected with the std::memory_order_relaxed tag. Relaxed atomic operation will impose no restrictions on other memory operations. The only effect that remains, is that the operation is itself still atomic.
Release-Acquire Ordering
An atomic store operation can be tagged with std::memory_order_release and an atomic load operation can be tagged with std::memory_order_acquire. The first operation is called (atomic) store-release while the second is called (atomic) load-acquire.
When load-acquire sees the value written by a store-release the following happens: all store operations sequenced before the store-release become visible to (happen before) load operations that are sequenced after the load-acquire.
Atomic read-modify-write operations can also receive the cumulative tag std::memory_order_acq_rel. This makes the atomic load portion of the operation an atomic load-acquire while the atomic store portion becomes atomic store-release.
The compiler is not allowed to move store operations after an atomic store-release operation. It is also not allowed to move load operations before atomic load-acquire (or load-consume).
There is no atomic load-release or atomic store-acquire. A load’s order must be memory_order_relaxed, consume, acquire, or seq_cst. A store’s order must be relaxed, release, or seq_cst. Any other order is undefined behavior, not a silent relaxed operation.
Release-Consume Ordering
This combination is similar to release-acquire, but this time the atomic load is tagged with std::memory_order_consume and becomes (atomic) load-consume operation. This mode is the same as release-acquire with the only difference that among the load operations sequenced after the load-consume only these depending on the value loaded by the load-consume are ordered.
Fences
Fences also order memory operations between threads. A fence may be a release, acquire, acquire-release, or sequentially consistent fence.
A release fence synchronizes with an acquire fence only through an intervening synchronization, often a relaxed atomic store that the other thread’s atomic load reads. Stores sequenced before that release fence then happen before loads sequenced after the acquire fence.