Previous | Next --- Slide 11 of 52
Back to Lecture Thumbnails
pajamajama

To summarize this slide: If "Hello" is printed, then this guarantees that A = 1 by the time P2 loads A and does the (A == 0) check. Note that the check occurs after the line of code that writes 1 to B. Based out what we output, this means that the read of B (checking if B = 0) occurred before the write to B (writing B = 1), which ensures that the write to A (assigning 1 to A) already occurred at the point we load and read A.

We can reason similarly with the case when we output "World" by saying that we know B = 1 by the time the B is loaded. Because of what we output, this means that the read to A occurred before the write to A and so by the time we check if B = 0, the write to B has already occurred. Furthermore, because this system is sequentially consistent (maintains W->R, R->R, R->W, W->W orderings), it is not possible to print out both "Hello" and "World".

The case on the top right and bottom right both will not print anything out. This is because 1 has been written to both A and B, so when each variable is loaded in their respective lines, none have value 0, so nothing is printed.

rrp123

If we relax memory consistency however, we can print out all 4 combinations, since at that point a write to A from thread 1 may not be seen by thread 2, and similarly, a write to B from thread 2 may not be seen by thread 1. Thus, both loops will progress but their local copies of B and A respectively will be 0, since the writes haven't committed to memory.