Memory consistency and observable order
Use small execution arguments to distinguish per-location coherence from ordering across multiple addresses.
On this page
The question for today
What you’ll understand
- Define sequential consistency through a compatible total order.
- Analyze the two-location store-buffering example with explicit assumptions.
- Separate a language memory model from an instruction-set memory model.
Two cores can agree on the order of writes to each individual location while still observing surprising relationships between different locations. A memory consistency model specifies which complete observations are allowed. It is a contract for reasoning about executions, not merely a diagram of cache plumbing. Start with a tiny litmus test, state the model and operation types, and ask whether any permitted ordering explains the proposed result.
Sequential consistency is an existence claim
For sequential consistency, the observed results must be explainable by one total order of the operations that preserves each participating thread's program order. The machine need not literally execute every instruction serially. It may overlap and speculate internally so long as the visible result meets the contract. Initial values can be treated as preceding the tested operations. A proof therefore looks for a compatible ordering rather than guessing a processor's exact internal timing.
Weaker architectural models preserve selected dependencies and ordering constraints while permitting other outcomes. That flexibility can support implementation techniques such as buffering stores or overlapping memory requests. It does not mean all reorderings are allowed. The RISC-V RVWMO specification, for example, defines precise constraints and explicit ordering mechanisms. Check the relevant instruction types, dependencies, memory attributes, and fences; a slogan such as 'weak architecture' is not a complete execution model.
The store-buffering cycle
Let x and y start at zero. Thread A stores one to x and then loads y; thread B stores one to y and then loads x. Under sequential consistency, both loads cannot return zero. Each zero observation would place its load before the other thread's store, while program order places each thread's own store before its load. Combining those requirements forms a cycle, contradicting a single total order.
Some weaker hardware models permit the both-zero observation for suitable ordinary memory operations. A store can remain buffered while a later load of another location proceeds, provided the architecture's rules allow it. This is not evidence that coherence failed for x or y. At source level, use a legal litmus test with appropriately chosen atomic operations or an architecture-specific harness. Ordinary racy C++ variables cannot be justified by a hardware-level argument about possible reordering.
Publication needs a synchronization relationship
A producer often writes a payload and then signals readiness. A consumer that observes readiness needs a documented guarantee before reading the payload. In suitable source-level code, a release operation and an acquire operation that synchronizes with it can establish the required happens-before relationship. A relaxed atomic flag is not a universal publication mechanism. Likewise, adding a hardware fence in one place does not automatically repair arbitrary source-language races or every surrounding algorithm.
The waves lab combines two fixed-frequency sine waves with phase difference and amplitude ratio controls. Set equal amplitudes and predict cancellation near a 180-degree phase difference. Use this only to contrast a continuous phase variable with a logical ordering relation: memory order is not a wave phase, and changing the slider cannot test a litmus program. After inspecting the cancellation, return to the four-operation cycle and identify which ordering edges a real synchronization design must establish.
Work it through
Ruling out an SC outcome
For the two-thread store-buffering test with x=y=0 initially, show why the outcome A reads y=0 and B reads x=0 is impossible under sequential consistency.
Name operations Wx, Ry, Wy, and Rx. Preserve each thread's order: Wx before Ry, and Wy before Rx. No clock measurements are needed.
For Ry to read the initial zero with no later resets, Ry must occur before Wy in the explaining total order. Similarly Rx must occur before Wx.
Join the four constraints: Wx before Ry before Wy before Rx before Wx. A strict total order cannot contain that cycle, so at least one load must return one. Different weaker models require their own analysis.
The SC exclusion is a logical proof under stated assumptions, not an empirical claim that every real machine or every atomic memory order forbids both zeroes.
The living notebook
Explore the model
Interactive lab · waves
Continuous phase versus logical order
Change phase difference and amplitude ratio in the two-sine-wave model. Predict equal-amplitude cancellation near 180 degrees. The instrument illustrates phase only; it does not execute a memory-order litmus test or represent cache propagation.
- Sum at x = 0
- 0.866
- Resultant amplitude
- 1.732
- Phase difference
- 60°
Two waves share the same frequency and wave number. Playback advances t in sin(2x − t) + a sin(2x − t + phase). Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forMemory-model reasoning needs explicit ordering edges rather than a visual analogy with timing or phase.
Pause & reason
Check your understanding
Your turn
Try it yourself
Assume four atomic operations in the same store-buffering test must obey sequential consistency. Construct a permitted order in which A reads zero and B reads one, and explain each read.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Run A's store and load before B's store and load.
Compare with the solution
Choose Wx, Ry, Wy, Rx. Ry occurs before the store to y, so A reads the initial zero. Rx follows Wx, so B reads one. Both threads' store-before-load order is preserved. This is one permitted explaining order, not a guarantee that a particular scheduler will choose it or that the same reasoning applies to arbitrary weaker atomic operations.
Keep the idea
Take it with you
- Coherence concerns locations; consistency constrains complete observations across locations.
- Sequential consistency requires a compatible total order preserving program order.
- Source-language synchronization and architecture-level fences belong to distinct contracts.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.