Memory Models & Atomics

the store buffer

/ store buffer -> stor BUH-fer /

When a CPU core writes to memory, it usually does not wait for that write to reach the cache, let alone main memory — that would be agonizingly slow. Instead the write is dropped into a small, fast, per-core queue called the store buffer, and the core charges ahead with the next instruction. The store drains from the buffer into the cache later, in the background. Think of an outbox tray: you drop a letter in and walk away; it gets mailed eventually.

This one piece of hardware is the concrete REASON behind most of the surprising memory reordering you read about. Because a store sits in the buffer before it is globally visible, a younger load to a DIFFERENT address can complete first — the famous store-to-load reordering that even x86 TSO allows. A core can also peek at its OWN pending stores in its buffer (store-to-load forwarding), so it sees its own writes immediately even though other cores cannot yet. Drain that buffer and you have restored ordering; a full memory fence, or a seq_cst store, essentially forces the buffer to flush before proceeding, which is exactly why those operations cost more.

Tying it back: the store buffer is why release/acquire is not automatic and why fences exist. It is also the kernel of the classic store-buffer litmus test — two threads each storing then loading the other's variable can BOTH read the stale value, because each store is still parked in its own buffer when the load runs. Understanding the store buffer turns the memory model from a list of arbitrary rules into a picture of what the hardware is physically doing.

Core A: store x=1 (sits in A's buffer); then load y -> reads 0 (y not yet stored) Core B: store y=1 (sits in B's buffer); then load x -> reads 0 (x still in A's buffer) Both see 0 — not a bug, just two store buffers not yet drained.

The store-buffer effect in the flesh: each core's pending write is invisible to the other until the buffer drains.

The store buffer is invisible to single-threaded code and to cache coherence (coherence still keeps each address's writes ordered); it is a CONSISTENCY effect, and a fence's cost largely IS the cost of draining it.

Also called
write buffer寫入緩衝區