5.2.26 · D1C++ Programming

Foundations — std - atomic — lock-free operations

2,304 words10 min readBack to topic

This page assumes you know nothing. Before you can read the parent note (topic), you need a small pile of ideas. We build them one at a time, each earning the next.


1. Memory, a value, and a variable

Picture a row of school lockers. Each locker has a number painted on it (the address) and a note inside (the value).

Figure — std - atomic — lock-free operations

Why the topic needs this: everything atomics do is about one box that two people reach into. If you don't picture the box, "shared variable" is just words.


2. Thread — a worker with its own hands

The parent note's whole problem exists because a thread can be paused between its tiny steps. See std::thread and std::async for how threads are created.


3. A single line of code is many machine steps

Figure — std - atomic — lock-free operations

Why the topic needs this: the "lost update" bug lives in the gap between these three steps. If you think counter++ is one step, the bug is impossible to understand.


4. Read-Modify-Write (RMW) and the lost update

Figure — std - atomic — lock-free operations

Follow the figure: the red timeline shows Thread B loading 5 while Thread A is mid-flight. This interleaving is the enemy. An atomic RMW makes steps 1–2–3 an indivisible blink so no one can slip in between.


5. Atomic — indivisible

Why the topic needs this: std::atomic<T> is exactly the tool that turns the 3-step RMW into one uncuttable step.


6. Mutex — the padlock we want to avoid


7. Lock-free — and its cousins

Figure — std - atomic — lock-free operations

Why the topic needs this: "lock-free operations" is literally the title. It's the payoff — atomicity without the padlock's waiting — but you must know it is not the same as "no thread ever waits" (that is wait-free).


8. Cache and coherency (MESI) — why one core can't just win

Figure — std - atomic — lock-free operations

See Cache Coherency MESI and False Sharing. Why the topic needs this: this exclusive-ownership grab (an E/M state guarded by invalidations) is how the hardware provides atomicity with no software lock.


9. Compare-And-Swap (CAS) — the gate


10. Memory order — the second promise

Why the topic needs this: correctness across threads needs both atomicity (§5) and ordering (this). One without the other still lets bugs through.


How the foundations feed the topic

Read the map bottom-up: the ideas at the top are the raw building blocks, and every arrow means "is needed to understand." A memory cell plus a thread and its private register give you the three-step read-modify-write, whose interleaving causes the lost-update bug. That bug forces the idea of an atomic (indivisible) operation. Atomicity, combined with wanting to escape the mutex padlock, and made possible by cache/MESI ownership, yields lock-free operation and the compare-and-swap gate. Finally atomicity pairs with memory order for cross-thread visibility. All of these arrows converge on the std::atomic topic node — the parent note you are preparing to read.

Memory cell

Thread

Register

Read Modify Write

Lost update bug

Atomic indivisible

Mutex padlock

Lock free

Cache and MESI

Compare And Swap

Memory order

std atomic topic


Equipment checklist

Test yourself — reveal only after answering.

What is stored in a memory cell, and what labels it?
A value (a number) inside; an address labels it.
Why can two threads collide even on simple code?
The OS can pause a thread between the tiny machine steps of one line.
Name the three steps hidden inside counter++.
load into a register, add 1, store back to memory.
What is a register and who can see it?
A tiny fast slot inside a CPU core; private to that core until stored back.
Define a read-modify-write and give one example.
An op that reads, changes based on the read, then writes back — e.g. x++.
What does "atomic" guarantee about intermediate states?
None are visible; the op is seen as fully done or not started.
What cost does a mutex add that atomics try to avoid?
Waiting in line while another thread holds the lock.
What progress does lock-free guarantee, and what does it NOT?
Some thread always progresses (system-wide); it does NOT prevent an individual thread from starving.
Rank obstruction-free, lock-free, wait-free from weakest to strongest.
obstruction-free < lock-free < wait-free (wait-free bounds every thread's own steps).
Name the four MESI states.
Modified, Exclusive, Shared, Invalid.
Which MESI state(s) let a core write, and what does it broadcast to get there?
Modified or Exclusive; it broadcasts an invalidate so other copies become Invalid.
State what CAS does when the box no longer holds expected.
It does not write; it loads the current value into expected and returns false.
What are the six C++ memory orders, and which is the default?
relaxed, consume, acquire, release, acq_rel, seq_cst — default is seq_cst.
What does memory order control that atomicity alone does not?
The visibility and ordering of surrounding non-atomic writes across threads.