A model for portable concurrency
Before the 2011 standard, C plus plus had no defined multithreaded semantics. The modern C plus plus memory model specifies how threads interact through atomic objects and synchronization, so well written code is portable across compilers and processors.
Atomics and memory orders
Ordinary objects accessed concurrently with a write give a data race, which is undefined behavior. To share data safely you use atomic types or locks. Each atomic operation carries a memory order argument that selects how strongly it orders surrounding code.
- seq cst is the default and gives a single total order.
- acquire and release pair to create happens before edges.
- relaxed guarantees atomicity only, with no ordering.
Synchronizes with
A release store synchronizes with an acquire load that reads its value, establishing happens before between the writing and reading threads. This is the same conceptual machinery as the Java model but exposed with explicit, tunable ordering.
Key idea
The C plus plus memory model makes concurrent sharing well defined only through atomics or locks, with per operation memory orders that tune how strongly each access is ordered.