Skip to content

std::atomic (C++11)

In a Nutshell

A template class that guarantees read and write operations are indivisible, preventing data races when multiple threads concurrently access the same variable.

#include <atomic>

Core API Cheat Sheet

OperationSignatureDescription
Constructoratomic() noexcept = default;Default constructor (value is uninitialized)
AssignmentT operator=(T desired) noexcept;Atomically writes the given value
Readoperator T() const noexcept;Atomically reads and returns the current value
Storevoid store(T desired, memory_order order = memory_order_seq_cst) noexcept;Atomic write
LoadT load(memory_order order = memory_order_seq_cst) const noexcept;Atomic read
ExchangeT exchange(T desired, memory_order order = memory_order_seq_cst) noexcept;Atomically replaces the old value and returns it
Compare-and-exchangebool compare_exchange_weak(T& expected, T desired, ...) noexcept;Weak CAS, may spuriously fail
Compare-and-exchangebool compare_exchange_strong(T& expected, T desired, ...) noexcept;Strong CAS, fails only on a genuine mismatch
Atomic addT fetch_add(T arg, memory_order order = memory_order_seq_cst) noexcept;Atomically adds and returns the old value (integer/pointer)
Lock-free checkbool is_lock_free() const noexcept;Checks if the current type is lock-free

Minimal Example

cpp
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>

std::atomic<int> cnt{0};

int main() {
    std::vector<std::jthread> pool;
    for (int i = 0; i < 10; ++i)
        pool.emplace_back([] { for (int n = 0; n < 10000; ++n) cnt++; });
    std::cout << cnt << '\n'; // 输出 100000
}

Embedded Applicability: High

  • Properly aligned integer and pointer types typically map directly to hardware atomic instructions, with zero extra overhead
  • is_lock_free() allows runtime confirmation of whether the implementation is truly lock-free, avoiding implicit system calls
  • Replaces bulky mutexes, ideal for lightweight state synchronization between interrupts and the main loop
  • Overly large custom structures may degrade into internally locked implementations, which we must carefully avoid

Compiler Support

GCCClangMSVC
4.43.119.0

See Also


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

Built with VitePress