memory_order — controls the ordering guarantees of atomic operations.
1std::atomic<int> x{0}, y{0};2std::atomic<int> z{0};34// relaxed — no ordering guarantees5x.store(1, std::memory_order_relaxed);67// release — previous writes visible to acquire8x.store(1, std::memory_order_release);910// acquire — sees writes before release11if (y.load(std::memory_order_acquire) == 1) {12 // x.store(1) is guaranteed visible13}1415// acq_rel — both acquire and release16y.fetch_add(1, std::memory_order_acq_rel);1718// seq_cst — total order (default, strongest)19z.store(1, std::memory_order_seq_cst);
Orderings (weakest to strongest):
Rule: Use seq_cst unless you have a specific reason and proof of correctness.