|
|
|
|
|
by deadbeeves
956 days ago
|
|
I don't really get it. Why is { P0:r0 = 1; P1:r0 = 1 } not an allowed final state? What code code could the compiler generate that would definitely prevent the threads from interleaving their loads and stores on any given architecture? |
|
In the example given, the result is that both writes happened before both reads, which directly contradicts the source. There's a valid explanation for why it happens, but it's still paradoxical.
I remember at work a C++ standards committee member was giving examples of atomic and how to use them safely with different memory models, when someone pointed out his toy example for relaxed order was wrong. It took 5 people debating for a week to figure out what a safe/correct behavior would be. For a 10 line sample class.
As times your second question, the article recommends Hans Boehm's proposal to add a no-op conditional branch after the read. I guess it forces the load to be resolved and enforces a sequenced-before behavior on the individual threads. So at least one read must resolve before one write in the example.