std::atomic — thread-safe atomic operations.
1std::atomic<int> counter{0};23void increment() {4 counter.fetch_add(1, std::memory_order_relaxed);5}67// Wait until value is set8counter.wait(0); // blocks until counter != 0
Memory orders:
relaxed: no ordering guaranteesacquire: subsequent reads see writesrelease: prior writes visible to othersseq_cst: total order (default)Lock-free guarantee:
1if (counter.is_lock_free()) {2 // guaranteed lock-free on this platform3}
When to use: