Closed jammingyu closed 7 months ago
I mention exactly that in the book:
If both store operations happen before either of the load operations, it’s possible that neither thread ends up accessing S. However, it’s impossible for both threads to access S and cause undefined behavior, since the sequentially consistent ordering guarantees only one of them can win the race.
The example guarantees there is no undefined behavior. It prevents a situation where both threads concurrently access the data. It does not guarantee that at least one thread accesses it.
Okay.
I think I unconsciously misinterpreted the purpose of example because I looked SeqCst example below link and this book at the same time,
https://en.cppreference.com/w/cpp/atomic/memory_order
Thanks for you reply :)
The content that the question is about
the example of ch3 for SeqCst
https://marabos.nl/atomics/memory-ordering.html#seqcst
The question
I think the example used is not appropriate for SeqCst. SeqCst guarantees that all operations are sequencially consistant globally, but that doesn't mean context switch does not happen between store and load operation.
therefore below order is possible in the example code
For the proof, if I run the below test, there are some cases where DATA is actually zero, which implies that both threads never accessed to data.