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
这篇关于易失性读取发生在易失性写入之前吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:易失性读取发生在易失性写入之前吗?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01