问题
Thread A runs x.store(1, std::memory_order_release)
first,
then thread B runs x.load(std::memory_order_acquire)
.
x
in thread B is not guaranteed to read 1 stored by A.
If I use memory_order_seq_cst
, will it be guaranteed to read 1?
回答1:
There is no difference between memory orderings with regards to load/store of one atomic variable. This is because std::memory_order
specifies how regular, non-atomic memory accesses are to be ordered around an atomic operation.
Read std::memory_order for full details. In particular:
All modifications to any particular atomic variable occur in a total order that is specific to this one atomic variable.
回答2:
Thread A runs x.store(1, std::memory_order_release) first, then thread B runs x.load(std::memory_order_acquire).
How can you know that one thread did an operation after another?
Essentially by testing a shared state? Like an atomic?
So the way to determine if the hypothesis is true would be... to test the conclusion: is "read 1 stored by A" true or false.
So the question reads exactly like a tautology (if x is 2+2, is x equal to 2+2) or a circularity.
来源:https://stackoverflow.com/questions/52926915/atomic-load-store-with-memory-order