Curious
Curious

Reputation: 21510

Memory ordering or read-modify-write operation with (read/write)-only memory order

Executing the following is an atomic RMW operation

auto value = atomic.fetch_or(value, order);

When order is std::memory_order_acq_rel we know that the load of the previous value in the atomic will acquire any release operations that might have happened previously on the same atomic. And the write will release the current thread's writes for threads that acquire the same atomic variable. Same with std::memory_order_seq_cst.

But what is the expected behavior of the write part of the RMW operation with respect to memory ordering when you use std::memory_order_acquire? Similarly, what is the expected behavior of using std::memory_order_release for the load side of the RMW operation?

Upvotes: 5

Views: 797

Answers (1)

LWimsey
LWimsey

Reputation: 6647

Using std::memory_order_acquire on an atomic RMW is an acquire-only operation.
Since it does not 'release' anything, it cannot be part of a synchronizes-with relationship with another atomic acquire operation (on the same atomic variable) that occurs later.
Therefore, the equivalent for the store part is std::memory_order_relaxed

Similar reasoning for using std::memory_order_release on an RMW. It does not acquire anything, so the ordering equivalent for the load part is 'relaxed'

Upvotes: 6

Related Questions