atomic load store with memory order

落花浮王杯 提交于 2019-12-11 06:10:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!