修改顺序是否有助于 happens-before 关系?

Does modification order contribute to happens-before relationship?

// Thread 1
// do A
x.store(1, std::memory_order_release); // operation1

// Thread 2
// do B
x.store(2, std::memory_order_release); // operation2

// Thread 3
x.load(std::memory_order_acquire); // operation3

我了解到如果thread3读取thread1写入的值,release和acquire操作synchronized-withA的效果可见thread3.
但如果情况是这样呢:

1 和 3 之间是否存在发生前关系?
或者本质上,修改顺序是否有助于 happens-before 关系?

在您的示例中,您显示线程 1 和线程 2 都以完全相同的方式在 x 上运行。所以线程1和线程3的关系应该和线程2和线程3的关系完全一样

您的示例没有显示任何代码行,这些代码行会限制三个操作实际发生的顺序。换句话说,仅从这三个语句来看,无法判断加载操作是 return 12 还是 [=12= 的某个先前值].

如果我对您的问题的理解正确,则不会因为您使用的内存顺序而对整体顺序没有额​​外保证。根本没有阻止允许这种情况发生。

没有。操作 1 和操作 3 之间没有 happens-before 关系。

来自[atomics.order]/2

An atomic operation A that performs a release operation on an atomic object M synchronizes with an atomic operation B that performs an acquire operation on M and takes its value from any side effect in the release sequence headed by A.

... 不幸的是,根据 [intro.races]/5:

,操作 2 不在以操作 1 为首的发布顺序中

A release sequence headed by a release operation A on an atomic object M is a maximal contiguous sub-sequence of side effects in the modification order of M, where the first operation is A, and every subsequent operation is an atomic read-modify-write operation.

因此,我们无法在操作 1 和操作 3 之间构建任何 inter-thread happens-before relationship between Operation 1 and other operations, so there is no happens-before relationship