问题描述
我试着理解为什么这个例子是一个正确同步的程序:
I try to understand why this example is a correctly synchronized program:
a - volatile
Thread1:
x=a
Thread2:
a=5
因为存在冲突访问(有一个写入和读取)因此在每个顺序一致性执行必须发生 - 在该访问之间的关系之前。
假设顺序执行之一:
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?
推荐答案
不,之前的(以同步顺序)易失性读取同一变量的易失性写入不一定发生-before 易失性写入。
No, a volatile read before (in synchronization order) a volatile write of the same variable does not necessarily happens-before the volatile write.
这意味着它们可能处于数据竞争状态,因为它们是未发生的冲突访问关系。如果这是真的,几乎所有程序都包含数据竞争:)但它可能是一个规范错误。永远不应将易失性读写视为数据竞争。如果程序中的所有变量都是易失性的,则所有执行都是顺序一致的。请参阅
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
这篇关于易失性读取是否发生在易失性写入之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!