Does exchange or compare_and_exchange reads last value in modification order?

末鹿安然 提交于 2019-12-01 06:51:54

问题


I am reading C++ Concurrency in Action by Anthony Williams. At section "Understanding Relaxed Ordering" it has:

There are a few additional things you can tell the man in the cubicle, such as “write down this number, and tell me what was at the bottom of the list” (exchange) and “write down this number if the number on the bottom of the list is that; otherwise tell me what I should have guessed” (compare_exchange_strong), but that doesn’t affect the general principle.

Does it mean that such operations always read last value in modification order (if there are no additional inter-thread happens before constraints)? I.e. are there some caches updates/etc (even in relaxed ordering)?


回答1:


Yes. The C++ Standard says (29.3/10):

Atomic read-modify-write operations shall always read the last value (in the modification order) written before the write associated with the read-modify-write operation.

Both exchange and successful compare_exchange_{weak,strong} operations are read-modify-write operations.



来源:https://stackoverflow.com/questions/14795126/does-exchange-or-compare-and-exchange-reads-last-value-in-modification-order

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