← Lessons

quiz vs the machine

Silver1090

Concurrency

The Happens Before Relation Deep Dive

The partial order that underpins every memory model guarantee.

5 min read · intro · beat Silver to climb

A partial order, not a clock

Happens before is the central abstraction shared by the Java and C plus plus models. It is a partial order on memory actions: some pairs are ordered and some are not. It says nothing about real time, only about visibility and which reorderings are legal.

How edges are formed

Happens before is built by combining two ingredients and taking the transitive closure.

  • Program order orders actions within one thread.
  • Synchronizes with edges connect a release in one thread to a matching acquire in another, for example an unlock to a later lock, or a release store to an acquire load that reads it.
  • Chaining these gives transitivity: if A is before B and B is before C, then A is before C.

What it buys you

If a write happens before a read, the read is guaranteed to observe that write. If two conflicting actions are unordered, the outcome is a data race and undefined or unspecified. So correct concurrency is the craft of placing enough synchronization edges to order everything that matters.

Key idea

Happens before is a transitive partial order from program order plus synchronizes with edges, and it determines which writes a read is guaranteed to see.

Check yourself

Answer to earn rating on the learn ladder.

1. Why is happens before called a partial order?

2. What property lets edges chain into longer guarantees?