Derek Zhang
Derek Zhang

Reputation: 101

atomic load store with memory order

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?

Upvotes: 1

Views: 863

Answers (2)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

There is no difference between memory orderings with regards to load/store of one atomic variable. This is because std::memory_order specifies how memory accesses, including 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.

Upvotes: 1

curiousguy
curiousguy

Reputation: 8270

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.

Upvotes: 1

Related Questions