C++中的Java挥发性的目的相同?

发布于 2025-01-23 13:44:32 字数 235 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2025-01-30 13:44:32

没有替代品。

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.

挽袖吟 2025-01-30 13:44:32

现代主流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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文