gpt4 book ai didi

c++ - 释放/获取语义 wrt std::mutex

转载 作者:可可西里 更新时间:2023-11-01 16:39:34 25 4
gpt4 key购买 nike

我正在阅读 n3485 中定义的 C++ 内存模型,它讨论了释放/获取语义,根据我的理解以及定义 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.

将防止在当前读/写完成之前或之后重新排序读/写。第一个(获取)将确保当前正在完成的读取不会被其后的任何读/写重新排序,后者(释放)将确保当前的写入不会被之前的读/写操作重新排序

现在可以说 std::mutex::lock 将具有 acquire 语义并且 std::mutex::unlock本质上具有 release 语义?

在标准中,我可以在部分下找到它

30.4.1.2 互斥锁类型 [thread.mutex.requirements.mutex]

11 Synchronization: Prior unlock() operations on the same object shall synchronize with (1.10) this operation.

据我了解, 同步并未在标准中明确定义,但它似乎是一种先于 关系,查看两个正在评估的两个语句但是,根据我对获取/释放语义的理解,不同的线程更多地与内存重新排序有关。 同步也可以称为释放/获取语义?

那么释放/获取语义是否不仅适用于加载/存储操作的重新排序,还适用于操作的线程内交错?

在关于内存模型的标准部分中,它主要讨论了两个线程交错的有序关系。这是否也适用于内存排序还有待解释。

谁能澄清一下?

最佳答案

Now can it be said that std::mutex::lock will have acquire semantics and that std::mutex::unlock essentially has release semantics?

,这是正确的。

From what I understand synchronize with is not explicitly defined in the standard

好吧,理论上第 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). [Note: ...]

另一方面,这听起来不像是一个非常正式的定义。然而,在第 1.10/10 段中间接给出了一个更好但隐含的:

An evaluation A is dependency-ordered before an evaluation B if

— 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

— for some evaluation X, A is dependency-ordered before X and X carries a dependency to B.

[ Note: The relation “is dependency-ordered before” is analogous to “synchronizes with”, but uses release/- consume in place of release/acquire. —end note ]

因为“类似于”关系通常是对称的,我想说上面的“是依赖顺序之前”的定义间接地提供了一个定义“同步”——尽管你可能会正确地反对注释是非规范的;不过,这似乎是预期的定义。

我对 同步 关系的直觉是,它发生在由存储特定值的线程执行的写入(原子)操作与第一个(原子)之间读取该值的操作。该操作也可能在同一个线程中。

如果这两个操作在不同的线程上,则同步关系会在操作上建立跨线程顺序。

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.

对我来说,这似乎与上面给出的解释相符。具有释放语义的操作(解锁、存储)将与获取语义的操作(锁定、加载)同步。

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和acquire语义描述了一些操作的性质; synchronizes-with 关系(实际上)是一种关系,它以明确定义的方式在具有获取或释放语义的操作之间建立。

所以在某种意义上,synchronizes-with 是这些操作语义的结果,我们使用这些语义来实现指令的正确排序并限制 CPU 或编译器将执行。

关于c++ - 释放/获取语义 wrt std::mutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15313296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com