根据the Java Memory Model (JMM):

当且仅当所有顺序一致的执行都没有数据竞争时,一个程序才是correctly synchronized.

如果程序被正确同步,则该程序的所有执行将看起来是顺序一致的(§17.4.3).

I don't see how the the fact that no SC execution has data races guarantees that every execution has no data races (which means that every execution is SC).
Is there a proof of that?


我发现:

  1. A blog post by Jeremy Manson (one of the authors of the JMM).
    The following paragraph might mean that the guarantee is provided by causality (but I don't see how):

    因此,这个模型背后有一种直觉.如果要证明读操作返回写操作的值这一事实是合理的,则可以在以下情况下执行此操作:

    a) That write happened before it, or
    b) You have already justified the write.

    该模型的工作方式是,从具有属性a)的所有操作都合理的执行开始,然后开始迭代地调整操作.因此,在上面的第二个示例中,您创建的连续执行证明了0、2、3、4、1的合理性.

    这一定义的可爱之处在于,它相当直观,也保证了DRF节目的SC.

  2. Foundations of the C++ Concurrency Memory Model.
    This article describes C++ memory model (which has similarities with the JMM).
    Section 8 of the article has a proof of a similar guarantee for C++:

    定理8.1.If a program allows a type 2 data race in a consistent execution, then there exists a sequentially consistent execution, with two conflicting actions, neither of which happens before the other.8

    In effect, we only need to look at sequentially consistent executions in order to determine whether there is a data race in a consistent execution.
    [...]
    8 The latter is essentially the condition used in [25] to define “correctly synchronized” for Java.

    不幸的是,我不确定这一证明是否适用于JMM,因为以下情况不起作用:

    考虑<T中不包含数据竞争的最长前缀P.请注意,P中的每个加载必须看到一个存储,该存储在同步或发生之前的顺序中位于它之前.

    在我看来,上述情况在JMM中不起作用,因为因果关系允许读操作返回稍后的存储.

推荐答案

证据就在The Java Memory Model by J.Manson, W.Pugh and S.Adve:

9.2.1正确同步的程序仅显示顺序一致的行为

引理2.Consider an execution E of a correctly synchronized program P that is legal under the Java memory model. If, in E, each read sees a write that happens-before it, E has sequentially consistent behavior.

Proof:
Since the execution is legal under the memory model, the execution is happens-before consistent and synchronization order consistent.
A topological sort on the happens-before edges of the actions in an execution gives a total order consistent with program order and synchronization order. Let r be the first read in E that doesn’t see the most recent conflicting write w in the sorted order but instead sees w′. Let the topological sort of E be αw′βwγrδ.
Let αw′βwγr′δ′ be the topological sort of an execution E′. E′ is obtained exactly as E, except that instead of r, it performs the action r′, which is the same as r except that it sees w; δ′ is any sequentially consistent completion of the program such that each read sees the previous conflicting write.
The execution E′ is sequentially consistent, and it is not the case that w′ ‒hb→ w ‒hb→ r, so P is not correctly synchronized.
Thus, no such r exists and the program has sequentially consistent behavior. □

定理3.If an execution E of a correctly synchronized program is legal under the Java memory model, it is also sequentially consistent.

Proof: By Lemma 2, if an execution E is not sequentially consistent, there must be a read r that sees a write w such that w does not happen-before r. The read must be committed, because otherwise it would not be able to see a write that does not happen-before it. There may be multiple reads of this sort; if so, let r be the first such read that was committed. Let Eᵢ be the execution that was used to justify committing r.
The relative happens-before order of committed actions and actions being committed must remain the same in all executions considering the resulting set of committed actions. Thus, if we don’t have w ‒hb→ r in E, then we didn’t have w ‒hb→ r in Eᵢ when we committed r.
Since r was the first read to be committed that doesn’t see a write that happens-before it, each committed read in Eᵢ must see a write that happens-before it. Non-committed reads always sees writes that happens-before them. Thus, each read in Eᵢ sees a write that happens-before it, and there is a write w in Eᵢ that is not ordered with respect to r by happens-before ordering.
A topological sort of the actions in Eᵢ according to their happens-before edges gives a total order consistent with program order and synchronization order. This gives a total order for a sequentially consistent execution in which the conflicting accesses w and r are not ordered by happens-before edges. However, Lemma 2 shows that executions of correctly synchronized programs in which each read sees a write that happens-before it must be sequentially consistent. Therefore, this program is not correctly synchronized. This is a contradiction. □

Java相关问答推荐

Cosmos Change Feed Process Lag远远超过收集中的记录数量

使用包私有构造函数强制子类Java类

为什么我们不能实现两个接口,其中一个接口有相同的签名,其中一个接口有默认的实现在java?'

有关手动创建的包的问题

Java 21 struct 化连接货币,需要可预知的子任务异常排序

是否在允许数组元素为空时阻止 idea 为空性警告?

如何调整工作时间日历中的时间

如何在ApachePOI中将图像添加到工作表的页眉?

生成桥方法以解决具有相同擦除的冲突方法

由于在生成器模式中使用泛型,lambda表达式中的返回类型错误

在处理2个映射表时,没有更多的数据可从套接字读取

Java在操作多个属性和锁定锁对象时使用同步和易失性

我的代码是线程安全的吗?[Java、CAS、转账]

在线程Java中调用Interrupt()之后调用Join()

Java中的发布/订阅-Long Live和Short Live Publisher,哪种方法是正确的?

我无法在我的Spring Boot应用程序中导入CSV依赖项

spring 更新多项管理关系

如何在Selenium上继续使用最新的WebDriver版本

设置背景时缺少Android编辑文本下划线

如何正确使用java.time类?