How Memory Ordering Works: Store Buffers, Barriers, and Why x86 Is Not Sequentially Consistent
Two threads can each write a flag and then read the other thread's flag, and both reads can return the old value. Nothing in the source swapped those operations. The processor did, because a store can sit in a store buffer while a later load goes to the cache. That is memory ordering: the rules for which program-order pairs of memory operations other cores are allowed to observe out of order.
This is not the same problem as cache capacity or false sharing. Those change how fast a line moves. Ordering decides whether a write is visible before another write, and whether a read is allowed to pass a write the same core has not yet drained. Mutexes and futexes hide the issue. Lock-free flags, rings, and sequence counters do not.
What sequential consistency promised
Sequential consistency, Lamport's 1979 model, says there is one total order of all memory operations, consistent with each thread's program order, and every read sees the last write in that order. If thread A writes x = 1 then reads y, and thread B writes y = 1 then reads x, sequential consistency forbids both reads returning 0. One of the two writes must sit earlier in the global order, and the other thread's later read must observe it.
Real cores do not implement that model for ordinary loads and stores. A store that has retired from the reorder buffer can still be invisible to other cores. A load can satisfy from a forwarding path or from a cache line while an older store to a different address is still buffered. The architectural memory model is the contract that says which of those observations are legal.
The store buffer is the x86 loophole
On x86 and AMD64, ordinary stores leave the core into a store buffer. The buffer lets the core continue without waiting for cache ownership. Loads to the same address snoop that buffer and see the local store. Loads to other addresses do not wait for older stores to become globally visible.
That single permission is store-load reordering, and it is the reordering x86 allows for normal aligned mov. Intel's memory-ordering rules for write-back memory state the rest of the picture: loads are not reordered with older loads, stores are not reordered with older stores, and a store is not reordered with an older load. Locked instructions, including the implicit lock on an xchg, are not passed by ordinary loads or stores. Non-temporal stores, string operations, and write-combined memory are the documented exceptions, not the common case.
The classic store-buffering litmus test is the one sequential consistency forbids:
Thread A Thread B
x = 1 y = 1
r1 = y r2 = x
Both r1 and r2 can be 0. Each core forwarded its own store into its buffer and let the later load hit cache before the other core's store drained. Dekker's algorithm needs a fence or a locked operation between the store and the load for this reason. On x86 that fence is mfence, or a locked instruction such as lock addl $0, (%rsp), which is what compilers often emit for a sequentially consistent atomic.
Total store order, and what it does not cover
x86 is a total-store-order machine. There is a single global order of stores. A core's own stores enter that order in program order. Once a store is in the global order, later loads on other cores see it, and they do not see an older store to the same address after a newer one.
Message passing therefore works on x86 hardware without an extra fence, provided the compiler did not move the accesses:
Thread A Thread B
data = 42 if (flag == 1)
flag = 1 use(data)
The store to flag cannot pass the store to data, and the load of data cannot pass the load of flag. If B sees flag == 1, it sees data == 42. That is a hardware fact about ordinary write-back stores. It is not a C or C++ fact. A compiler may still keep flag in a register, sink the store to data, or delete the reload of flag if the variables are not atomic and not volatile. The CPU model starts only after the compiler has emitted instructions.
Weaker machines reorder the other pairs
ARM and RISC-V do not promise total store order for ordinary memory. Stores can pass older stores. Loads can pass older loads. A load can pass an older store to a different address, just as on x86. Independent loads can also be satisfied out of order from a store queue on some microarchitectures, which is load-load reordering from the programmer's point of view.
On those machines the message-passing example is not safe at the instruction level. Thread A needs a release barrier between data = 42 and flag = 1, so the data store is visible before the flag store enters the coherence order. Thread B needs an acquire barrier between the load of flag and the load of data, so the data load cannot be satisfied by a value observed before the flag load. ARM's dmb ish is the heavy form. ARMv8 also has stlr and ldar, store-release and load-acquire, which are one-way barriers and map directly onto this pattern.
Apple silicon can run a core in a TSO mode for translated x86 code. That mode is a compatibility switch, not the native application model. Native ARM code is still written against the weak model.
Barriers are one-way constraints, not flushes of the universe
A barrier does not push every dirty line to DRAM, and it does not stall every core. It orders this core's accesses relative to a point in its own instruction stream.
- A store-store barrier keeps older stores before younger stores in the global order. Release semantics are this constraint plus a dependency on the releasing store.
- A load-load barrier keeps older loads before younger loads. Acquire semantics are this constraint on the acquiring load.
- A full barrier also closes the store-load window. Sequential consistency needs that window closed, which is why
memory_order_seq_cstis more expensive than acquire/release on x86 and why it often becomesmfenceor a locked operation.
Linux encodes the same split. smp_store_release and smp_load_acquire compile to plain stores and loads on x86, and to barrier-paired accesses on ARM. smp_mb is the full barrier. The kernel documentation is explicit that these are ordering primitives, not cache-maintenance operations. Cache maintenance is clflush, dc civac, and their relatives, used for DMA and self-modifying code, not for locks.
What the compiler is allowed to do
volatile in C and C++ suppresses some compiler elision and forces a memory access, but it is not an inter-thread barrier. Two volatile stores can still be reordered by an ARM core. A non-volatile store next to a volatile store can still be moved by the compiler on either architecture.
C++ atomics are the portable contract. memory_order_relaxed is atomicity only: no torn read of a correctly sized aligned object, and no compiler deletion of the access, but no ordering against other accesses. memory_order_release on a store and memory_order_acquire on the load that observes that store form the message-passing pair. memory_order_seq_cst adds a single total order of all sequentially consistent operations, which is what the store-buffering litmus needs.
On x86, release and acquire atomics typically emit ordinary mov. The hardware already supplies those directions. The compiler barrier is the part you still needed. On ARM, the same source emits stlr and ldar, or a dmb. Writing a lock-free flag with plain stores because it works on a laptop is testing TSO, not the language.
Where this shows up in real code
A mutex critical section is an acquire on entry and a release on exit. The unlock store cannot become visible before the writes inside the section, and the next lock holder cannot read those locations before it has observed the unlock. You do not add fences inside a correctly locked section to publish data to the next holder. The futex word is the publication point; the kernel wake-up does not replace the userspace release store.
A single-producer queue published with a head index is the same pattern as the flag. The producer writes slots, then release-stores the index. The consumer acquire-loads the index, then reads slots. Relaxed arithmetic on a counter that nobody uses as a publication edge is fine. Relaxed publication of the index is not, except on a machine and a compiler you control completely.
Sequence locks, used by the kernel for mostly-read data, depend on the same edges: writers publish with a release store of an odd sequence, update, then release-store the even sequence. Readers acquire-load the sequence, copy, and check. A missing barrier lets a reader observe a new sequence and old payload, or a torn mix, even when the sequence counters themselves are not torn.
RCU has a different contract. The grace period waits until pre-existing readers are done; it is not a substitute for a release store on the pointer publication. The publisher still needs the initialization of the new object to be ordered before the store of the pointer other CPUs load.
Misconceptions
x86 is not sequentially consistent. Store-load reordering is observable, and Intel documents it. Locked instructions close that hole; plain stores do not.
A barrier does not flush the cache. It orders this core's accesses. Other cores still pull lines through the coherence protocol when they miss. False sharing can make that protocol expensive without changing the ordering rules.
Disabling interrupts does not order memory across cores. Interrupt masking is local. Another core does not observe it. Device drivers that program a device and then kick a doorbell need a barrier or an ordered MMIO mapping between the descriptor writes and the doorbell, because MMIO is not ordinary write-back memory.
A quiet race detector does not prove the hardware model. A race detector such as ThreadSanitizer instruments the language memory model. It will not flag a correct atomic release/acquire pair, and it will not run the binary on an ARM store queue. Hardware litmus tests and the architectural manual are the check for the instruction stream.
What to remember
Other cores observe stores through a coherence order that is not your program order. x86 preserves every pair except store then load to different addresses, and even that exception breaks sequential consistency. ARM preserves almost none of them unless the instruction stream says so. Compilers reorder earlier than either core does. Acquire and release match the publication pattern; a full barrier is for the cases, such as store buffering, where a store must be visible before a later load. Mutexes already contain those edges. Lock-free publication has to say them out loud.
Related on this site: how CPU caches and false sharing work, how futexes implement the mutex slow path, how RCU grace periods work, and how virtual memory and page tables work.