Memory Consistency Models
A memory consistency model defines the order in which one core's memory operations become visible to others in a shared-memory system.
Order Is Not Obvious
In a multiprocessor, coherence guarantees all cores eventually agree on the value of each single location. A memory consistency model answers a harder question: in what order do operations to different locations become visible across cores? This matters because hardware reorders and buffers memory operations for speed, and without a defined model, programmers cannot reason about concurrent code.
Sequential Consistency
The strongest, most intuitive model is sequential consistency: the result of execution is as if all cores' operations were interleaved into one global order that respects each core's own program order. It is easy to reason about but expensive, because it forbids many performance optimizations like buffering a write while later reads proceed. Almost no high-performance hardware provides it directly.
- Sequential consistency: one global interleaving, per-core program order preserved
- Total store order: reads may pass buffered writes to other addresses
- Weak/relaxed: few guarantees without explicit ordering instructions
Relaxed Models
Real processors relax the rules for speed. Total store order, used by x86, lets a core's writes sit in a store buffer so later reads to other addresses can proceed first, but keeps writes in order. Weaker models used by ARM and other architectures relax further, allowing extensive reordering. The weaker the model, the more the hardware can optimize, and the more care software must take.
Fences and Atomics
To regain control where it matters, architectures provide memory barriers (fences) that forbid reordering across them, and atomic read-modify-write operations that other cores cannot interrupt. Lock-free data structures and synchronization primitives are built from these. Getting consistency right is one of the subtlest parts of concurrent programming, and the model is a contract the hardware and the compiler must jointly honor.