PUBLICATION

Visible before it's ready

L318 MINRACE CONDITIONS
PREDICT

Publisher: obj.x = 42; shared = obj. Reader: if (shared != null) use(shared.x). No fences, no volatile. What can the reader observe?

The publisher runs obj.x = 42 and then shared = obj. Those are two plain stores, and neither the compiler nor the CPU promises to make them visible in that order. The CPU parks stores in a store buffer. On ARM and POWER it may drain them out of order, so shared = obj can reach memory first. x86 drains its store buffer in order, but the compiler or JIT is still free to swap the two stores. A reader that loads shared, sees the object, and loads x gets whatever was in that memory before: zero, or garbage. A release store makes every earlier store visible before the reference; an acquire load on the other side stops the reader from pulling the field load above the reference load. Both halves are needed. Here shared.x depends on the pointer, and mainstream CPUs (all but the old DEC Alpha) will not load it early. The danger on the reader side is the compiler and the language memory model, which promise nothing without acquire. With an independent flag and data field, the CPU can hoist the load too.

▶ SIMULATION
PUBLISHER
store buffer: empty
pc: start
MEMORY
shared = null
obj.x = (not allocated)
READER
r = —
v = —
pc: start
✓ in memory: shared != null ⇒ obj.x is initialized✓ a reader that sees the reference sees initialized fields
SCHEDULE
01[PUB]PUB: obj = alloc() (x = 0)
02[PUB]PUB: obj.x = 42 → store buffer
03[PUB]PUB: shared = obj → store buffer
04[HW]HW: commit shared = obj (reordered ahead of x)
05[RDR]RDR: r = shared → obj
06[RDR]RDR: v = r.x → 0
07[RDR]RDR: use(v)
08[HW]HW: commit obj.x = 42 (too late)
Start of torn publication scenario. 8 steps.
// Publisher:                    // Reader:
obj = new Thing();              while (shared == null) {}
obj.x = 42;                     use(shared.x);   // may see 0
shared = obj;   // plain store: may become visible before obj.x

Watch the MEMORY panel. In TORN PUBLICATION, shared = obj lands while obj.x is still 0, the first invariant turns red at that moment, and the reader walks straight into it. In FIX: RELEASE / ACQUIRE the release drains x = 42 before the reference can land, and the same commit step cannot break it. RELEASE WITHOUT ACQUIRE shows the other half: a correct writer and a reader whose field load ran early, which the language model allows without acquire.

BREAK IT NOT YET

The publisher's stores sit in a store buffer; on a weakly ordered CPU like ARM the hardware may commit them to memory in either order. Order the steps so the reader uses an uninitialized x.

GOAL: the reader uses x = 0 from a published object

FIX IT NOT YET

Rewrite publish so a reader that acquire-loads ref and finds it non-null always sees x = 42 and y = 7. The grader’s store buffer may commit pending stores in any order the rules allow, at any moment, and checks memory after every commit.

Mental model:

Memory does not see your program order. It sees stores in whatever order the compiler and the store buffer let them out, and loads in whatever order the reader pulls them in. A reference is safe to publish only when there is a release on the way out and an acquire on the way in. The pair is what creates the happens-before edge; either half alone leaves a window.

IN PRODUCTION

Lazy-initialized singletons and caches, config reloads that swap a global pointer, lock-free queues that hand objects between threads, any “set the fields then set the flag” handshake in C, C++, Java, Go or Rust without the matching atomic. Symptom: a rare read of zeros or nulls from an object that was fully built, on many-core hardware, never in the debugger.

IN INTERVIEWS

“Is this double-checked locking correct?” Say what the store buffer does to the two stores, name release/acquire as the pair that fixes it, and give the language's spelling: volatile in Java, memory_order_release/acquire in C++, a channel or sync.Once in Go. Then point out that the reader side needs its half too; most candidates stop at the writer.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: LOCKS, ATOMICS, LOCK-FREE →