All Atomics and Memory Ordering
All Atomics and Memory Ordering
A compact scan page for standard atomic types, operations, fences, and memory-order vocabulary.
All Atomics and Memory Ordering
A compact scan page for standard atomic types, operations, fences, and memory-order vocabulary.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
std::atomic<T>std::atomic_flagstd::atomic_ref<T>loadstoreexchangecompare_exchange_weakcompare_exchange_strongfetch_add, fetch_subfetch_and, fetch_or, fetch_xorwait, notify_one, notify_allstd::atomic_thread_fencestd::atomic_signal_fenceis_lock_freeis_always_lock_freestd::memory_ordermemory_order_relaxedmemory_order_consumememory_order_acquirememory_order_releasememory_order_acq_relmemory_order_seq_cstseq_cst until you can justify weaker orders precisely.#include <atomic>
#include <thread>
int main() {
std::atomic<bool> ready{false};
int result = 0;
std::jthread worker([&] {
result = 42;
ready.store(true, std::memory_order_release);
});
while (!ready.load(std::memory_order_acquire)) {
}
return result;
}
This is the standard acquire/release pattern: the store publishes both the flag and the preceding write to result, and the acquire load makes that published value visible once the flag becomes true.
memory_order_seq_cst when you need the simplest correct reasoning modelrelease on the publishing store and acquire on the consuming load for one-way handoff patternsrelaxed only when the value is independent and no cross-thread ordering is required#include <atomic>
#include <thread>
int main() {
std::atomic<bool> ready{false};
int value = 0;
std::jthread producer([&] {
value = 42;
ready.store(true, std::memory_order_release);
});
while (!ready.load(std::memory_order_acquire)) {
}
return value == 42 ? 0 : 1;
}