All Atomics and Memory Ordering

All Atomics and Memory Ordering

A compact scan page for standard atomic types, operations, fences, and memory-order vocabulary.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

All Atomics and Memory Ordering

Core atomic types

Common atomic operations

Fences and lock-free queries

Memory-order vocabulary

Practical rules

Small worked example

#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.

Practical order guide

Example in practice

#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;
}