简体   繁体   English

发布/获取语义wrt std :: mutex

[英]Release/Acquire semantics wrt std::mutex

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 : 我正在阅读n3485中定义的C ++内存模型,它讨论了发布/获取语义,根据我的理解,以及本博客中给出的定义:

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? 现在可以说std::mutex::lock将具有获取语义,并且std::mutex::unlock基本上具有发布语义?

In the Standard I can find this under section 在标准中,我可以在部分下找到它

30.4.1.2 Mutex types [thread.mutex.requirements.mutex] 30.4.1.2互斥体类型[thread.mutex.requirements.mutex]

11 Synchronization: Prior unlock() operations on the same object shall synchronize with (1.10) this operation. 11同步:对同一对象的先前unlock()操作应 (1.10)此操作同步

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? 任何人都可以澄清一下吗?

Now can it be said that std::mutex::lock will have acquire semantics and that std::mutex::unlock essentially has release semantics? 现在可以说std :: mutex :: lock将具有获取语义,并且std :: mutex :: unlock基本上具有发布语义?

Yes , this is correct. 是的 ,这是正确的。

From what I understand synchronize with is not explicitly defined in the standard 据我所知, 同步并未在标准中明确定义

Well, in theory Paragraph 1.10/8 is probably meant to give the definition of synchronizes with : 那么,理论上第1.10 / 8段可能是为了给出与以下内容同步的定义:

Certain library calls synchronize with other library calls performed by another thread. 某些库调用另一个线程执行的其他库调用同步 For example, an atomic store-release synchronizes with a load-acquire that takes its value from the store (29.3). 例如,原子存储释放从存储获取其值的load-acquire 同步 (29.3)。 [Note: ...] [注意: ...]

On the other hand, this does not sound like a very formal definition. 另一方面,这听起来不像是一个非常正式的定义。 However, a better, though implicit one is indirectly given in Paragraph 1.10/10: 然而,在第1.10 / 10段中间接给出了一个更好但隐含的更好的方法:

An evaluation A is dependency-ordered before an evaluation B if 评估B if 之前 ,评估A是依赖性排序

— A performs a release operation on an atomic object M, and, in another thread, B performs a consume operation on M and reads a value written by any side effect in the release sequence headed by A, or - A对原子对象M执行释放操作,并且在另一个线程中,B对M执行消耗操作并读取由A标记的释放序列中的任何副作用写入的值,或者

— for some evaluation X, A is dependency-ordered before X and X carries a dependency to B. - 对于某些评估X,A在X之前是依赖排序的,而X对B具有依赖性。

[ Note: The relation “is dependency-ordered before” is analogous to “synchronizes with”, but uses release/- consume in place of release/acquire. [注意: 关系“依赖于顺序排序”类似于“与...同步”,但使用release / - 代替释放/获取。 —end note ] - 尾注]

Since the " is analogous to " relationship is most often symmetric, I would say that the above definition of " is-dependency-ordered before " indirectly provides a definition of " synchronizes with " as well - although you might correctly object that notes are non-normative; 由于“ 类似于 ”关系通常是对称的,我会说上面的“ is-dependency-ordered before ”的定义间接提供了“ 同步 ”的定义 - 尽管你可能正确地反对那些注释是非-normative; still, this seems to be the intended definition. 不过,这似乎是预期的定义。

My intuition of the synchronizes with relationship is that it occurs between a write (atomic) operation performed by one thread that stores a certain value and the first (atomic) operation that reads that value. 我对关系同步的直觉是它发生在一个存储某个值的线程执行的写(原子)操作和读取该值的第一个 (原子)操作之间。 That operation might as well be in the same thread. 该操作也可能在同一个线程中。

If the two operations are on different threads, then the synchronizes-with relation establishes a cross-thread ordering on operations. 如果两个操作在不同的线程上,则synchronize-with关系在操作上建立跨线程排序。

In the Standard I can find this under section 在标准中,我可以在部分下找到它

30.4.1.2 Mutex types [thread.mutex.requirements.mutex] 30.4.1.2互斥体类型[thread.mutex.requirements.mutex]

11 Synchronization: Prior unlock() operations on the same object shall synchronize with (1.10) this operation. 11同步:对同一对象的先前unlock()操作应 (1.10)此操作同步

To me, this seems compatible with the interpretation given above. 对我来说,这似乎与上面给出的解释兼容。 An operation with release semantics (unlock, store) will synchronize with an operation of acquire semantics (lock, load). 具有释放语义(解锁,存储)的操作将与获取语义(锁定,加载)的操作同步。

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? 同步也可以称为发布/获取语义?

Release and acquire semantics describe the nature of some operations; 释放和获取语义描述了某些操作的本质; the synchronizes-with relationship is (indeed) a relationship which is established between operations that have acquire or release semantics, in a well-defined way. 同步关系(实际上)是以明确定义的方式在具有获取或释放语义的操作之间建立的关系

So in a sense, synchronizes-with is a consequence of the semantics of those operations, and we use those semantics to achieve the correct ordering of instructions and constraint the possible reordering that the CPU or the compiler will perform. 因此,在某种意义上, synchronize-with是这些操作的语义的结果,我们使用这些语义来实现指令的正确排序并约束CPU或编译器将执行的可能的重新排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM