One Relation Settles Every Argument About What a Thread Is Allowed to See
Last timeWhen the Other Thread Sees It
Happens-before is the whole guarantee. If a chain of edges runs from a write to a read, the read sees it. If no chain does, nothing is promised at all.
The relation
The previous lesson ended with a question. The flag is now properly published,
so the waiting thread sees it become true. Is the message behind the flag
visible as well?
Arguments about this tend to go in circles because each side reasons from a
different informal picture. There is a precise rule, it is short, and it
settles every such question.
A read is guaranteed to see a write if and only if a chain of happens-before
edges leads from the write to the read. If no chain does, nothing whatever is
promised. The read may see the new value, the old value, or in some languages a
value nobody ever wrote.
Note what the rule does not mention: real time. One event occurring ten minutes
before another in wall-clock terms creates no edge and no promise. Ordering
between threads comes only from the specific operations listed below.
What creates an edge
The list is short enough to memorise, which is the point of having it.
| creates an ordering edge | safe to rely on | |
|---|---|---|
| two statements in the sa | 1 | 1 |
| releasing a lock, then a | 1 | 1 |
| writing an atomic, then | 1 | 1 |
| starting a thread, then | 1 | 1 |
| a write happening earlie | 0 | 0 |
| two threads holding diff | 0 | 0 |
| a write followed by a sl | 0 | 0 |
Add one more rule and the relation is complete: the edges are transitive. If
the first event is ordered before the second and the second before the third,
the first is ordered before the third, even across three different threads.
Chains are what make the relation useful, because the interesting cases are
never a single edge.
The lesson stops here
3 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents