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
这篇关于易失性读取发生在易失性写入之前吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:易失性读取发生在易失性写入之前吗?
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
