std::atomic
std::atomic<T> is a variable whose reads and writes are indivisible to other threads. A data race on an atomic is not a data race in the standard's sense: the operations are well-defined. Use it for flags, counters, and simple published pointers — not as a replacement for a mutex around a vector.
Detailed Explanation
counter++ on an int is a race if two threads do it. counter.fetch_add(1) on an std::atomic<int> is not. The machine uses an atomic instruction (or a lock if T is large). You get a well-defined total for that counter.
atomic<T> requires T to be trivially copyable. atomic<int>, atomic<bool>, atomic<T*> are the everyday types. atomic<shared_ptr<T>> exists from C++20. There is no atomic<string>.
Operations: load, store, exchange, compare_exchange_weak / strong, and for integers fetch_add, fetch_sub, fetch_and, fetch_or. The compound assignments ++, += are defined and atomic.
Each operation takes a memory-order argument (see memory-order). The default is memory_order_seq_cst, the strongest: all threads see a single total order of seq_cst operations. Weaker orders (acquire, release, relaxed) can be faster and are easier to get wrong. Start with the default. Reach for relaxed only on a pure counter that publishes nothing else.
is_lock_free() tells you whether the implementation uses a lock under the hood. A 16-byte atomic on a 64-bit platform may not be lock-free. atomic_flag is guaranteed lock-free and is the building block of a spinlock you should not write unless you measure.
Atomics do not compose. Two atomic ints updated "together" still need a mutex (or a carefully designed protocol) if a reader must see both updates as one. An atomic pointer to a vector does not make the vector's push_back safe.
A common pattern is a published pointer: thread A builds an object, then stores the pointer with release; thread B loads with acquire and reads the object. The acquire/release pair makes B see A's writes to the object. relaxed on that store would not.
Do not use atomics to paper over a race you have not named. If the shared state is a struct with several fields, use a mutex.
Code Examples
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
std::atomic<int> hits{0};
void bump() { hits.fetch_add(1, std::memory_order_relaxed); }
int main() {
std::vector<std::thread> ts;
for (int i = 0; i < 8; ++i) ts.emplace_back(bump);
for (auto& t : ts) t.join();
std::cout << hits.load() << '\n'; // prints: 8
}#include <atomic>
std::atomic<bool> stop{false};
void worker() {
while (!stop.load(std::memory_order_acquire)) {
// do work
}
}
int main() {
stop.store(true, std::memory_order_release);
}Common Gotchas
`atomic<T>` of a non-trivial T
will not compile. Use a mutex.
Atomic pointer, non-atomic object
publishing `T*` atomically does not make writes to `*p` race-free unless you pair orders correctly and no one writes after publish without a protocol.
`compare_exchange_weak` can fail spuriously
loop, or use `strong` when you are not already in a loop.
`volatile` is not atomic
it disables some optimisations for MMIO. It does not provide inter-thread synchronisation.