Figure 9-1.

A diagram showing happens-before relationships in a situation with two threads.

Each thread is represented by a sequence of boxes, one per operation. Within each thread, each box has an arrow to the next.

The first thread has four operations:

  1. lock: acquire-swap from 0 to 1,
  2. access protected data,
  3. release-swap from 2 to 0
  4. wake one

3 and 4 together form a group labeled “unlock”.

The second thread starts with a group of four operations labeled “lock”:

  1. compare-exchange loading 1
  2. acquire-swap from 1 to 2
  3. wait
  4. acquire-swap from 0 to 2

There is an arrow from the release operation on the first thread to the fourth operation on the second thread. The value 0 is emphasized in both boxes, to show that the arrow was created because of the acquire operation observing the 0 that the release operation stored.

The second thread continues with “access protected data”, and a group of two operations labeled “unlock”: release-swap 2 to 0, and wake one.