C++中的Java挥发性的目的相同?
我在Java中学习了domagile
关键字。当变量由一个特定线程编写时,它是确保在其他线程中可见性的一种均值。它通过删除某些变量的机器缓存并在某些情况下在某些情况下禁用CPU指令来实现此目的。
我注意到挥发性
在C ++中存在,而它具有不同的目的。我想知道C ++如何实现Java中volatile
的其他与cache相干性的实现。
I learned the volatile
keyword in Java. It serves as a mean to ensure visibility in other threads when a variable is written by one particular thread. It does this by removing machine caches of certain variables and disabling CPU instruction reordering under some cases at each write.
I noted that volatile
exists in C++, while it serves different purposes. I wonder how C++ implements cache coherence and other things that goes with volatile
in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有替代品。
C ++ 11从Java衍生出其内存模型,但明确承认某些Java选择证明是次优的。 Java的
挥发性
语义是其中之一,并且能够同步每个对象。C ++确实不是实现缓存相干。这是编译器和CPU的东西。在C ++级别,您已订购操作。 A 发生在 B之前。从某种意义上说,可以将其与
domagile
进行比较的情况是C ++原子。There is no replacement.
C++11 derives its memory model from Java, but it explicitly acknowledged that some Java choices turned out to be suboptimal. Java's
volatile
semantics are one of those, as was the ability to synchronize on every object.C++ does not implement cache coherence. That's something for compilers and CPU's. At the C++ level, you have ordered operations. A happens-before B. One such case, that could be compared with
volatile
in some sense, are C++ atomics.现代主流CPU具有连贯的缓存。这是通过缓存相干协议来照顾的。如果一个字段是(Java)挥发性,这是独立的。因此,普通载荷/商店也有一致性。尽管可能侵犯了链的连贯性更高。
Java挥发性的C ++版本将是一种原子,带有Menemy_order_seq_cst(默认值)。
Modern mainstream CPUs have coherent caches. That is taken care of by the cache coherence protocol. This is independent if a field is (Java) volatile or not. So there is also coherence on plain loads/stores. Although it can be that higher up the chain coherence is violated.
The C++ version of Java volatile would be an atomic with memory_order_seq_cst (which is the default).