skip to content

In C++, what goes wrong when two threads run ++count on a shared plain int, and what does std::atomic<int> change?

level: juniorimportance: must knowfreq 78%

answer

  1. load, add, store
  2. data race means undefined behavior
  3. one indivisible read-modify-write
  4. ++ is fetch_add underneath
  5. a = a + 1 is two operations

basics

~20 s

Two threads incrementing a plain int without synchronisation is a data race, which C++ makes undefined behavior; in practice increments get lost. On std::atomic<int>, ++count is one indivisible read-modify-write (fetch_add), so no increment is lost.

solid answer

~40 s

`++count` is a load, an add and a store. On a plain `int`, two threads doing that concurrently is a **data race** — two conflicting accesses, at least one not atomic, neither happening before the other — and the standard makes any data race undefined behavior, not just a wrong total. In practice both threads read the same old value and both store old+1, and the optimiser may keep the counter in a register. On `std::atomic<int>`, `++count` is one atomic read-modify-write, equivalent to `fetch_add(1) + 1`, with `memory_order::seq_cst` by default, so every increment lands. The trap is `count = count + 1` on the atomic: that is an atomic load and then a separate atomic store, race-free but still able to lose increments.

code

cpp · 20 lines
cpp
#include <atomic>
#include <cstdio>
#include <thread>

std::atomic<int> hits{0};
std::atomic<int> split{0};

void work() {
    for (int i = 0; i < 100000; ++i) {
        ++hits;               // one atomic read-modify-write: fetch_add(1)
        split = split + 1;    // an atomic load, then a separate atomic store
    }
}

int main() {
    std::thread a(work), b(work);
    a.join();
    b.join();
    std::printf("hits=%d split=%d\n", hits.load(), split.load());
}

go deeper

for a junior

Say the words data race and undefined behavior, and show that ++ on a std::atomic<int> is one indivisible read-modify-write that never loses an increment.

for a middle

Explain why a = a + 1 and check-then-act code still lose updates on an atomic, and name fetch_add and the seq_cst default behind the operators.

for a senior

Show how the optimiser exploits a race on a plain int, why the result has no defined output, and how a race detector confirms the bug before any fix is trusted.

for a principal

Treat every shared counter as a design decision: an atomic for independent counts, a mutex when the update spans several variables, and code review rules that reject unsynchronised shared writes.

## What `++count` really is On a plain `int`, the expression `++count` is not one step. The machine **loads** the current value, **adds** one and **stores** the result. Two threads that interleave those steps can both load 41, both compute 42 and both store 42: one increment vanishes. That is the everyday symptom, and it is why the total of 200,000 increments from two threads comes out lower and different on every run. ## Why the standard calls it undefined behavior C++ does not describe the lost update as "a wrong number". The standard defines a **data race**: two potentially concurrent **conflicting** actions on the same memory location (at least one of them a write), at least one of which is **not atomic**, where neither **happens before** the other. It then says plainly that any data race results in **undefined behavior**. Consequences that follow from that rule: - **Lost updates** are only the mild case. - The compiler may assume no other thread touches a non-atomic object without synchronisation, so it can keep `count` in a register for the whole loop, or fold 100,000 increments into a single `add`. - For types wider than the machine word, a reader can observe a **torn** value that no thread ever wrote. - Because the behavior is undefined, there is no "expected wrong answer" to reason about; a program with a race has no guaranteed output at all. ## What `std::atomic<int>` changes `std::atomic<int>` turns every access to the object into an **atomic operation**: indivisible with respect to every other atomic access to the same object, and never part of a data race. | Expression on `std::atomic<int> a` | What it is | Can it lose an increment? | |---|---|---| | `++a`, `a++` | `fetch_add(1)` — one read-modify-write | no | | `a += 1` | `fetch_add(1) + 1` | no | | `a.fetch_add(1)` | one read-modify-write, returns the old value | no | | `a = a + 1` | `a.load()`, then a separate `a.store(...)` | yes | | `int v = a;` | `a.load()` | — | Two rules make the first three rows safe. A read-modify-write must read the **last value in the modification order** of the object, so two `fetch_add` calls can never both see 41. And the operators use **`memory_order::seq_cst`** unless you call the member function with a weaker order yourself. ## The two-step trap The last row is the classic mistake. `a = a + 1` compiles, raises no warning and has no data race, because both the load and the store are atomic. But another thread can increment between them, and the store then overwrites that increment. The same shape hides in **check-then-act** code: ```cpp if (a < limit) ++a; // the check and the increment are separate operations ``` Two threads can both pass the check at `limit - 1` and both increment. When the update depends on the current value, use one read-modify-write (`fetch_add`, `fetch_sub`, `exchange`) or a `compare_exchange_weak` loop, which is covered with compare-and-exchange. ## Seeing it on a real compiler 1. Build the example below with `-std=c++17 -O2` and run it a few times: `hits` is always 200000, `split` is lower and changes from run to run. 2. Add a plain `int` incremented the same way and build with `-fsanitize=thread`: ThreadSanitizer, covered with the sanitizers, reports `WARNING: ThreadSanitizer: data race` with both threads' stacks. 3. Remove the plain `int` again before trusting any output, because a program with a data race has no defined output. ## What `std::atomic<int>` does not do - It does not make a **sequence** of operations atomic; only each single operation is. - It does not protect any **other** variable; ordering other memory is what memory orders are for. - It is not copyable: the copy constructor and copy assignment are deleted, so you read the value with `load()` and write it with `store()`.

  • Does std::atomic<int> make if (count < limit) ++count; safe?
    No. The comparison is one atomic load and the increment is a second atomic operation, so two threads can both see `limit - 1`, both pass the check and both increment. Either do the whole decision in a `compare_exchange_weak` loop that only stores when the value it read is still below the limit, or protect the check and the increment with one mutex.
  • Why can't you copy a std::atomic<int> into another one?
    `std::atomic`'s copy constructor and copy assignment are deleted. Copying one atomic into another would be a load from the first and a store into the second — two operations on two objects — and no single atomic operation covers both. You write the intent explicitly: `std::atomic<int> b{a.load()};` or `b.store(a.load());`, which makes the non-atomic pair visible in the code.

  • A race on an int only makes the count a little low, nothing worse.
  • count = count + 1 on a std::atomic<int> is as safe as ++count.
  • Incrementing an int is one instruction, so another thread cannot interleave with it.
  • Marking the shared int volatile makes each increment atomic.
  • Making the counter atomic also makes the plain variables written next to it race-free.