4.1.26 · D3Computer Architecture (Deep)

Worked examples — Memory models — sequential consistency, TSO, relaxed

3,692 words17 min readBack to topic

Before we start, three words we will use constantly, defined from zero:

Figure — Memory models — sequential consistency, TSO, relaxed

Look at the board: the yellow vertical arrow on the far left shows program-text order (top box "op 1" down to bottom box "op 4"). A reordering is when the hardware lets a lower box finish before a higher box — a curved arrow jumping backwards up the column. The blue curved arrow from "op 2 (Load y)" up past "op 1 (Store x)" is the single jump TSO allows. The two pink curved arrows (a store leaping past a store, a load leaping past a store) are the extra jumps only Relaxed allows.


The scenario matrix

Every cell below is covered by a worked example (Ex-N in the last column).

Cell class Concrete question SC TSO Relaxed Example
reorder SB test, outcome Ex-1, Ex-2
/ MP test, stale read Ex-3, Ex-4
+ coherence LB (load-buffer) test Ex-5
Degenerate: one thread any single-thread reorder (invisible) (invisible) (invisible) Ex-6
Same address (coherence only) many ops, one location ✗ always Ex-7
Limiting: fence everywhere SB + MFENCE restores SC Ex-8
Real-world word problem producer/consumer queue ✓ safe needs rel/acq Ex-9
Exam twist IRIW (4-thread) ✗(!) Ex-10

Ex-1 — under SC (forbidden)


Ex-2 — under TSO (allowed)

This is why Peterson and Dekker Locks need an explicit fence on x86.


Ex-3 — under TSO (still forbidden → publication is free)


Ex-4 — under Relaxed (allowed → stale read appears)


Ex-5 — under Relaxed (the Load-Buffer test)


Ex-6 — Degenerate case: a single thread (nothing is visible)


Ex-7 — Same-address case (coherence only)


Ex-8 — Limiting case: fence everywhere restores SC


Ex-9 — Real-world word problem: a one-slot mailbox


Ex-10 — Exam twist: IRIW (Independent Reads of Independent Writes)


Wrap-up

Recall Which single reordering distinguishes each pair of models?

SC vs TSO ::: store→load (SB test, Ex-1/Ex-2) TSO vs Relaxed ::: store→store or load→load (MP test, Ex-3/Ex-4) and load→store (LB, Ex-5) The IRIW surprise ::: TSO still forbids it (multi-copy atomic); only non-MCA relaxed allows it (Ex-10)

Related: parent topic · Hinglish version.