ContentsThe library

What Two Threads Can Break

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.

FIG 1Which pairs of operations create an edge
creates an ordering edgesafe to rely on
two statements in the sa11
releasing a lock, then a11
writing an atomic, then 11
starting a thread, then 11
a write happening earlie00
two threads holding diff00
a write followed by a sl00
The two marked rows are the ones people assume create an edge. Neither does. A sleep long enough that the value always arrives in practice is still a program with no guarantee, and it is the standard way a race survives for years before failing.

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 contents

This 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

The rest of this course

  1. 01Two Threads, Four Steps, and Six Different Programs You Did Not Write
  2. 02The Statement You Wrote Is Three Instructions and the Danger Lives Between Themopening only
  3. 03A Perfectly Correct Lock Around Exactly the Wrong Piece of Codeopening only
  4. 04Four Things Must All Be True, So Breaking Any One of Them Is Enoughopening only
  5. 05The Loop Is Still Spinning and the Flag Was Set Ten Minutes Agoopening only
  6. 06One Relation Settles Every Argument About What a Thread Is Allowed to Seeyou are here
  7. 07Write It Only If Nobody Moved It, and Try Again If They Didopening only
  8. 08Seven Lessons of Difficulty That All Disappear If Nobody Shares Anythingopening only

Read alongside