Does exchange or compare_and_exchange reads last value in modification order?
Asked Answered
W

1

3

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)?

Wilbur answered 10/2, 2013 at 5:49 Comment(2)
"read last value" last compared to what?Visitation
Related: Is a memory barrier required to read a value that is atomically modified? / Does hardware memory barrier make visibility of atomic operations faster in addition to providing necessary guarantees? (no, and RMWs aren't faster either). This part of the standard is how the formalism guarantees atomicity, that another write can't happen on this variable between the load and store. Nothing more, nothing less.Ullyot
S
7

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.

Signora answered 10/2, 2013 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.