问题
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