Skip to content
Tutorials Center
My learning

Topics

Lesson 137 of 171 ยท Concurrency

Atomics and the Memory Model

Learn C++ atomics - std::atomic, fetch_add, compare_exchange loops, memory orders like acquire and release, an atomic_flag spinlock, and when a mutex is the better choice.

A mutex makes a whole block of code exclusive: one thread at a time. That's often more than you need. If all you want is to bump a counter, flip a "done" flag or swap one pointer, locking and unlocking a mutex around that single operation is heavy machinery. Atomics are variables whose individual operations, such as a load, a store or an increment, happen as one indivisible step that no other thread can see half-finished. std::atomic<int> gives you a counter that many threads can increment at once without a lock and without a data race.

Atomics also come with the rules that make concurrent C++ well-defined at all: the memory model. It answers the question "when one thread writes something, when is another thread guaranteed to see it?" This lesson covers std::atomic and its operations, the compare_exchange loop that powers lock-free code, a simplified but accurate tour of memory orders, a spinlock built from std::atomic_flag, C++20's wait and notify, and clear advice about when atomics are the right tool and when a mutex is. It builds on Threads and Mutexes and Locks.

Why counter++ breaks across threads

counter++ on a plain int looks like one step, but the machine does three: load the value, add one, store the result. If two threads both load 41, both add one and both store 42, one increment is lost. Worse, the C++ standard says that two threads accessing the same non-atomic variable, with at least one writing and no synchronization, is a data race, and a data race is undefined behavior. The compiler is allowed to assume it never happens.

std::atomic<int> fixes both problems. Its ++ is a single read-modify-write operation, and concurrent access to an atomic is never a data race.

C++ A plain int versus std::atomic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The atomic total is always 8000. The plain total is usually lower, and different from run to run. The yield() call between the read and the write just makes thread switches land in the worst spot more often; real races need no invitation, they're just rarer and harder to reproduce. Never "fix" a race by hoping it's rare: the plain version is undefined behavior even on the runs where it happens to print 8000.

The std::atomic interface

std::atomic<T> lives in <atomic>. It works for bool, every integer and character type, pointers, and (since C++20) float and double. It also works for any trivially copyable type, such as a small struct, though large types are usually implemented with a hidden lock.

Operation What it does
a.load() / a.store(v) Read or write the value atomically. Plain a and a = v do the same.
a.exchange(v) Store v and return the old value, in one step.
a.fetch_add(n) / a.fetch_sub(n) Add or subtract and return the old value (integers, pointers, and floating point since C++20).
a.fetch_and(m) / fetch_or / fetch_xor Bitwise read-modify-write, return the old value (integers only).
++a, a++, a += n, a |= m, ... Shorthand for the fetch operations. ++a returns the new value, a++ the old one.
a.compare_exchange_weak(expected, desired) / _strong "If the value is still expected, set it to desired." Explained below.
a.wait(old) / a.notify_one() / a.notify_all() C++20: sleep until the value changes from old.
a.is_lock_free(), std::atomic<T>::is_always_lock_free Whether the hardware does this without a hidden lock.

Two things are deliberately missing. Atomics can't be copied or moved: std::atomic<int> b = a; doesn't compile, because "copy atomically from one place to another" isn't something hardware can do. Write std::atomic<int> b{a.load()}; instead. And a compound expression is not atomic as a whole: a = a + 1 is an atomic load followed by an atomic store, with a gap in between where another thread can sneak in. Use ++a or fetch_add.

The return value of fetch_add matters more than it seems. Because it returns the value from before your increment, and no two threads can get the same one, it's a perfect source of unique numbers:

C++ fetch_add hands out unique tickets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The tickets are always 100 through 107 with no duplicates; which thread got which number varies. Both lock-free checks print true on our 64-bit server, because an 8-byte Point fits in one machine word. A std::atomic of a 100-byte struct compiles too, but it quietly uses a lock, and on GCC it may need linking with -latomic.

Thread-safe is not the same as correct. Each atomic operation is safe on its own, but a sequence of them is not one transaction. if (stock.load() > 0) { stock--; } can sell the last item twice: both threads see 1, both decrement, and stock becomes -1. When a decision depends on the current value, use a single read-modify-write operation or a compare_exchange loop.

compare_exchange: the building block of lock-free code

fetch_add covers addition, but what about "store the maximum", "decrement only if positive" or "multiply by 3"? There is no fetch_max until C++26. The general tool is compare-and-swap (CAS), spelled compare_exchange_weak or compare_exchange_strong:

C++
bool ok = a.compare_exchange_strong(expected, desired);
// atomically:
//   if (a == expected) { a = desired; return true; }
//   else               { expected = a; return false; }   // expected is UPDATED

The typical pattern is a loop: read the current value, compute the new one, and try to install it. If another thread changed the value in the meantime, the CAS fails, refreshes expected with the value it actually found, and you compute again from that.

C++ An atomic maximum with a CAS loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Ten buyers compete for five items, and exactly five succeed every time; the stock never goes negative. The maximum is the same on every run because update_max can only ever raise the value.

weak or strong? compare_exchange_weak is allowed to fail spuriously: it may return false even when the value equaled expected, which happens on some processors (ARM, for example) where CAS is built from two instructions. That's harmless inside a loop, which retries anyway, and it can be slightly faster there. Use compare_exchange_strong when you call CAS once, outside a loop, and a false failure would be wrong. On x86 both compile to the same instruction.

The ABA problem. CAS checks that the value is equal to what you saw, not that nothing happened. If another thread changes A to B and back to A, your CAS succeeds as if nothing changed. With plain counters that's fine. With pointers in a lock-free stack it's a classic bug: the node at address A may have been freed and a new node allocated at the same address. Real lock-free data structures use version counters, hazard pointers or garbage collection schemes to handle this. Writing them is expert work; reach for a mutex-protected container first.

The memory model, simplified

Atomicity is half the story. The other half is visibility and ordering. Compilers reorder instructions, and processors execute and commit memory writes out of order, all for speed. Within one thread you never notice, because the reordering preserves that thread's own view. But another thread can observe writes in a surprising order unless something forbids it.

The pattern that matters most is publishing data: one thread prepares some data, then raises a flag; another thread waits for the flag, then reads the data. For that to work, "the data was written" must be visible to the reader before "the flag is set". Atomics express this with memory orders, an optional extra argument on every operation:

Order Meaning (simplified) Typical use
memory_order_seq_cst The default. All seq_cst operations appear in one global order that every thread agrees on. Everything, unless measured to be too slow.
memory_order_release On a store: every write this thread did before it becomes visible to a thread that acquire-loads this value. Publishing: "data is ready".
memory_order_acquire On a load: if it reads a value stored with release, everything before that store is visible after this load. Consuming: "is the data ready?"
memory_order_acq_rel Both, for read-modify-write operations. Locks, reference counts dropping to zero.
memory_order_relaxed Atomic, but no ordering with any other memory. Statistics counters, unique IDs.

(There's also memory_order_consume, which no compiler implements as designed; they all treat it as acquire, and C++26 deprecates it. Ignore it.)

Think of release and acquire as a matched pair: a release store is a "send", and an acquire load that sees that value is the matching "receive". Everything written before the send is guaranteed to be readable after the receive. The formal name is a happens-before relationship.

C++ Publishing data with release and acquire

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

results and report are not atomic, and that's fine: the producer only touches them before the release store, and the consumer only touches them after its acquire load has seen true. That's exactly the guarantee you need to hand data between threads without a lock. If you changed both orders to relaxed, the program would compile and probably still print the right thing on x86 (whose hardware ordering is strong), but it would have a data race on results, and on ARM it could genuinely print incomplete data.

Practical advice on memory orders

  • Start with the default, seq_cst. It's the easiest to reason about and it's correct whenever the weaker orders would be. On x86 a seq_cst load is as cheap as a plain load; only seq_cst stores cost a little more.
  • Use relaxed only for values nobody uses to decide whether other data is ready: hit counters, statistics, unique ID generators. fetch_add(1, std::memory_order_relaxed) is still a correct count.
  • Use release/acquire for "flag plus payload" handoffs like the one above, and only if profiling says seq_cst is a bottleneck.
  • If you're tempted to go further, read your platform's concurrency literature, and test with ThreadSanitizer (-fsanitize=thread), which detects data races at run time. See Undefined Behavior and Memory Safety.

volatile is not a substitute for any of this. In C++ it means "this memory may change outside the program" (hardware registers) and gives no atomicity and no inter-thread ordering. Code that uses volatile bool as a thread flag has a data race.

A spinlock with std::atomic_flag

std::atomic_flag is the simplest atomic type: a boolean that's guaranteed lock-free on every platform. Its key operation, test_and_set(), sets the flag and returns what it was before, in one step. That's exactly what a lock needs: whoever gets false back was first and owns the lock.

C++ A spinlock from atomic_flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Because Spinlock has lock() and unlock(), it satisfies the BasicLockable requirements, so std::lock_guard and std::scoped_lock accept it like any mutex. The total is always 100000.

A classic spinlock loops on test_and_set and burns CPU while it waits. That's only a good idea when the lock is held for a few nanoseconds and each thread has its own core. On a single core (like our server) a spinning thread wastes its whole time slice, because the lock holder can't run until it gives up the CPU. That's why this version calls wait, which lets the thread sleep. Honestly, std::mutex on Linux already spins briefly and then sleeps, so a hand-written spinlock rarely beats it. Write one to learn how locks work, then use std::mutex.

wait and notify: sleeping on an atomic (C++20)

C++20 added wait(old), notify_one() and notify_all() to every std::atomic and to atomic_flag. x.wait(old) blocks as long as x still equals old, without spinning, and returns once another thread changes the value and calls notify_*. It's a lightweight alternative to a condition variable when the condition is "this one value changed", and it's typically built on the operating system's futex.

C++ Waiting for a value to change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The output is three lines, stages 1, 2 and 3, because the worker pauses between updates. Unlike a condition variable, wait handles spurious wakeups itself: it only returns once it has seen a value different from old. But it doesn't queue up changes. If the value changes twice quickly, a waiter may only see the second one, so always re-read the atomic after waking and decide from the current value, as the loop does. And never forget the notify call: without it, a waiter may sleep forever even though the value changed.

atomic_ref and atomic shared_ptr

Two newer tools are worth knowing:

  • std::atomic_ref<T> (C++20) applies atomic operations to an ordinary object you don't own as an atomic, such as one element of a big std::vector<int>. While any atomic_ref to an object exists, all access to it must go through atomic_refs.
  • std::atomic<std::shared_ptr<T>> (C++20, supported since GCC 12) lets threads load and replace a shared pointer safely, which is handy for "readers grab the current configuration, a writer swaps in a new one". It's usually implemented with an internal lock, so it isn't lock-free.
C++
std::vector<int> histogram(256);
// many threads:
std::atomic_ref<int>{histogram[byte]}.fetch_add(1, std::memory_order_relaxed);

std::atomic<std::shared_ptr<const Config>> current_config;
auto config = current_config.load();                         // readers
current_config.store(std::make_shared<const Config>(new_settings)); // writer

When to use atomics

Atomics are excellent for a single variable with a simple job:

  • counters and statistics (fetch_add with relaxed),
  • flags such as "stop requested" or "initialization done" (though std::jthread's stop token already covers stopping),
  • unique ID and ticket generators,
  • reference counts (that's how std::shared_ptr works internally),
  • publishing one pointer to fully built data.

Use a mutex as soon as an invariant spans more than one variable ("the balance and the transaction log must agree"), when you need to wait for a complex condition, or when the critical section does real work. Two atomics are not an atomic pair, and a lock-free algorithm that coordinates several of them is far harder to get right than its mutex version, and often not faster. Lock-free programming is a specialist field; most fast concurrent C++ code is mutexes plus a few well-placed atomics.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Two threads each run x = x + 1; once on std::atomic<int> x{0}. What values can x have afterwards?

    x + 1 is an atomic load, and the assignment is a separate atomic store. Both threads can load 0 and store 1. Use ++x or x.fetch_add(1) for one indivisible step. There's no UB, because every access is atomic.
  2. 2. std::atomic<int> a{10}; What does int r = a.fetch_add(5); leave in r and a?

    The fetch_ operations return the value from before the modification. That's what makes fetch_add useful for handing out unique numbers.
  3. 3. int expected = 3; bool ok = a.compare_exchange_strong(expected, 7); but a holds 5. What happens?

    The values differ, so the swap fails and a is unchanged. A failed compare_exchange writes the value it actually found into expected, so a retry loop can recompute from the latest value.
  4. 4. A producer fills a vector, then does ready.store(true, std::memory_order_release). A consumer spins on ready.load(std::memory_order_acquire) until true, then reads the vector. Is this correct?

    Release on the store and acquire on the load that reads it create a happens-before relationship. Every write the producer made before the store is visible to the consumer after the load, on every platform.
  5. 5. Which job is a poor fit for atomics and better done with a mutex?

    An invariant that spans two variables can't be maintained by separate atomic operations. A mutex makes the whole update one critical section. The other three are single-variable jobs where atomics shine.

Summary

  • A data race (unsynchronized access to a non-atomic variable, with a write) is undefined behavior. Operations on std::atomic<T> never race.
  • Use load, store, exchange, fetch_add/fetch_sub/fetch_or and friends; the fetch operations return the old value. a = a + 1 is two operations, not one.
  • compare_exchange_weak in a loop implements any read-modify-write: on failure it updates expected to the current value. Use _strong for a single attempt.
  • Memory orders control visibility: seq_cst (default, simplest), release/acquire pairs for publishing data, relaxed for counters. volatile is not a threading tool.
  • std::atomic_flag plus test_and_set makes a spinlock; C++20's wait/notify lets threads sleep on an atomic value instead of spinning.
  • Atomics suit single variables: counters, flags, IDs, reference counts. Use a mutex when an invariant covers several variables.

Next: std::async, Futures and Promises, where you'll run tasks in the background and collect their results, and their exceptions, without managing threads by hand.