Looking at the c++ reference for std::memory_order would give us a deeper understanding of the acquire/release mechanism. acquire typically comes with a load operation with this memory order performs the acquire operation on the affected memory location, and release typically comes with a store. Thinking about their relationship, we could see that acquire ensures that all writes in other threads that release the same atomic variable are visible in the current thread, and release makes all writes in the current thread are visible in other threads that acquire the same atomic variable.
am i right that memory_order only ensures load of ready is after store of it? also is there any use case where memory_order semantic might need more than a store-load operation?
eknight7
@yangwu: From the link std::memory_order it is clear that the acquire/release semantics act like fences. It is not the case that memory order ensures load of ready after the corresponding store. memory_order_release ensures that no memory operations are reordered after this store, so all operations after this store will see this write to ready variable. memory_order_acquire ensures that no memory operations are reordered after this load, so all operations after this load will see this read from the ready variable.
Looking at the c++ reference for std::memory_order would give us a deeper understanding of the acquire/release mechanism.
acquire
typically comes with a load operation with this memory order performs the acquire operation on the affected memory location, andrelease
typically comes with a store. Thinking about their relationship, we could see thatacquire
ensures that all writes in other threads thatrelease
the same atomic variable are visible in the current thread, andrelease
makes all writes in the current thread are visible in other threads that acquire the same atomic variable.Another interesting link for students:
http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/
am i right that memory_order only ensures load of ready is after store of it? also is there any use case where memory_order semantic might need more than a store-load operation?
@yangwu: From the link
std::memory_order
it is clear that the acquire/release semantics act like fences. It is not the case thatmemory order
ensures load ofready
after the corresponding store.memory_order_release
ensures that no memory operations are reordered after this store, so all operations after this store will see this write toready
variable.memory_order_acquire
ensures that no memory operations are reordered after this load, so all operations after this load will see this read from theready
variable.