易失性读取发生在易失性写入之前吗?

Is volatile read happens-before volatile write?(易失性读取发生在易失性写入之前吗?)

本文介绍了易失性读取发生在易失性写入之前吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么这个例子是一个正确同步的程序:

I try to understand why this example is a correctly synchronized program:

a - volatile
Thread1:
x=a
Thread2:
a=5

因为存在冲突的访问(对 a 进行写入和读取),所以在每个顺序一致性执行中,访问之间的关系必须先发生.假设顺序执行之一:

Because there are conflicting accesses (there is a write to and read of a) so in every sequential consistency execution must be happens-before relation between that accesses. Suppose one of sequential execution:

1. x=a
2. a=5

1 发生在 2 之前,为什么?

Is 1 happens-before 2, why?

推荐答案

不,在同一变量的 volatile 写入之前(按同步顺序)进行 volatile 读取不一定 happens-before volatile写.

No, a volatile read before (in synchronization order) a volatile write of the same variable does not necessarily happens-before the volatile write.

这意味着它们可能处于数据竞争"中,因为它们是冲突的访问不是按发生前的关系排序的".如果这是真的,几乎所有程序都包含数据竞争:) 但这可能是一个规范错误.永远不应将易失性读写视为数据竞争.如果程序中的所有变量都是易失的,那么所有的执行都是顺序一致的.见 http://cs.oswego.edu/pipermail/concurrency-兴趣/2012-1月/008927.html

This means they can be in a "data race", because they are "conflicting accesses not ordered by a happens-before relationship". If that's true pretty much all programs contain data races:) But it's probably a spec bug. A volatile read and write should never be considered a data race. If all variables in a program are volatile, all executions are trivially sequentially consistent. see http://cs.oswego.edu/pipermail/concurrency-interest/2012-January/008927.html

这篇关于易失性读取发生在易失性写入之前吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:易失性读取发生在易失性写入之前吗?

基础教程推荐