I am reading the C++ memory model defined in n3485 and it talks about release/acquire semantics, which from what I understand, and also from the definitions given in this blog:
Acquire semantics is a property which can only apply to operations which read from shared memory, whether they are read-modify-write operations or plain loads. The operation is then considered a read-acquire. Acquire semantics prevent memory reordering of the read-acquire with any read or write operation which follows it in program order.
Release semantics is a property which can only apply to operations which write to shared memory, whether they are read-modify-write operations or plain stores. The operation is then considered a write-release. Release semantics prevent memory reordering of the write-release with any read or write operation which precedes it in program order.
is going to prevent reordering of reads/writes before or after the current read/write being done. The first (acquire) will make sure that the read currently being done is not reordered with any read/write coming after it, the latter (release) will make sure that the current write is not being reordered with read/write operations that come before it.
Now can it be said that std::mutex::lock
will have acquire semantics and that std::mutex::unlock
essentially has release semantics?
In the Standard I can find this under section
30.4.1.2 Mutex types [thread.mutex.requirements.mutex]
11 Synchronization: Prior
unlock()
operations on the same object shall synchronize with (1.10) this operation.
From what I understand synchronize with is not explicitly defined in the standard, however it seems to be a type of happens before relation looking at two statements being evaluated between two different threads, however, from my understanding of acquire/release semantics, this has more to do with memory reordering. synchronize with could also be called release/acquire semantics?
So do release/acquire semantics apply not only to reordering of load/store operations and also intra-thread interleaving of operations?
In the standard section about the memory-model it mostly talks about ordered relations in terms of two threads interleaving. This leaves open to interpretation as to whether this applies also to memory ordering.
Can anybody clarify?
See Question&Answers more detail:os