为什么我们使用 volatile 关键字?

发布于 2024-10-07 15:35:06 字数 199 浏览 2 评论 0原文

可能的重复:
为什么存在 volatile?

我从未使用过它,但我想知道为什么人们使用它?它到底有什么作用?我搜索了论坛,发现只有 C# 或 Java 主题。

Possible Duplicate:
Why does volatile exist?

I have never used it but I wonder why people use it? What does it exactly do? I searched the forum, I found it only C# or Java topics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

携余温的黄昏 2024-10-14 15:35:06

考虑这段代码,

int some_int = 100;

while(some_int == 100)
{
   //your code
}

当这个程序被编译时,编译器可能会优化这段代码,如果它发现程序从不曾经尝试更改some_int的值,所以它可能会想优化 while 循环,将其从 while(some_int == 100) 更改为 something ,这相当于 while(true) 以便执行速度更快(因为 while 循环中的条件似乎始终为 true)。 (如果编译器没有优化它,那么它必须在每次迭代中获取 some_int 的值并将其与 100 进行比较,这显然有点慢。)

然而,有时,(程序的某些部分)优化可能是不可取的,因为可能其他人正在从外部更改some_int 的值编译器不知道的程序,因为它看不到它;但这就是你的设计方式。在这种情况下,编译器的优化将不会产生期望的结果!

因此,为了确保获得所需的结果,您需要以某种方式阻止编译器优化 while 循环。这就是 volatile 关键字发挥作用的地方。你需要做的就是这个,

volatile int some_int = 100; //note the 'volatile' qualifier now!

换句话说,我会解释如下:

易失性告诉编译器,

“嘿编译器,我很不稳定,而你
知道,我可以被一些XYZ改变
你甚至不知道。那
XYZ 可以是任何东西。也许有一些
这个星球外的外星人叫
程序。也许有些闪电,有些
中断、火山等形式都可以
使我变异。或许。你永远不知道是谁
将会改变我!所以哦你
无知,别再扮演无所不知的样子
天哪,不敢碰代码
我在场的地方。好吗?”

好吧,这就是 volatile 阻止编译器优化代码的方式。现在在网上搜索一些示例。


引用自 C++ 标准 ($7.1.5.1/8)

[..] 挥发性是一个提示
实施以避免激进
涉及对象的优化
因为对象的值可能
通过无法检测到的方式进行更改
实施。[...]

相关主题:

使结构体变得易失性会使它的所有成员都易失性吗?

Consider this code,

int some_int = 100;

while(some_int == 100)
{
   //your code
}

When this program gets compiled, the compiler may optimize this code, if it finds that the program never ever makes any attempt to change the value of some_int, so it may be tempted to optimize the while loop by changing it from while(some_int == 100) to something which is equivalent to while(true) so that the execution could be fast (since the condition in while loop appears to be true always). (if the compiler doesn't optimize it, then it has to fetch the value of some_int and compare it with 100, in each iteration which obviously is a little bit slow.)

However, sometimes, optimization (of some parts of your program) may be undesirable, because it may be that someone else is changing the value of some_int from outside the program which compiler is not aware of, since it can't see it; but it's how you've designed it. In that case, compiler's optimization would not produce the desired result!

So, to ensure the desired result, you need to somehow stop the compiler from optimizing the while loop. That is where the volatile keyword plays its role. All you need to do is this,

volatile int some_int = 100; //note the 'volatile' qualifier now!

In other words, I would explain this as follows:

volatile tells the compiler that,

"Hey compiler, I'm volatile and, you
know, I can be changed by some XYZ
that you're not even aware of. That
XYZ could be anything. Maybe some
alien outside this planet called
program. Maybe some lightning, some
form of interrupt, volcanoes, etc can
mutate me. Maybe. You never know who
is going to change me! So O you
ignorant, stop playing an all-knowing
god, and don't dare touch the code
where I'm present. Okay?"

Well, that is how volatile prevents the compiler from optimizing code. Now search the web to see some sample examples.


Quoting from the C++ Standard ($7.1.5.1/8)

[..] volatile is a hint to the
implementation to avoid aggressive
optimization involving the object

because the value of the object might
be changed by means undetectable by an
implementation.[...]

Related topic:

Does making a struct volatile make all its members volatile?

宛菡 2024-10-14 15:35:06

在计算机编程中,特别是在 C、C++ 和 C# 编程语言中,使用 volatile 关键字声明的变量或对象通常具有与优化和/或线程相关的特殊属性。一般来说,易失性关键字旨在防止(伪)编译器对假设变量值不能“自行”更改的代码进行任何优化。 (c) 维基百科

http://en.wikipedia.org/wiki/Volatile_variable


编辑维基百科已经修复了有关 C++ 线程的废话,现在说

对易失性变量的操作不是原子的,它们也没有为线程建立适当的happens-before关系。这是在相关标准(C、C++、POSIX、WIN32)中指定的,并且 volatile 变量在当前的绝大多数实现中都不是线程安全的。因此,许多 C/C++ 团体不鼓励使用 volatile 关键字作为可移植的同步机制。

为了便于将来参考,下面的评论线程中关于 volatile 对于线程很有用的每一个声明要么是从其他语言完全错误的推断,要么是对非​​标准平台特定扩展(特别是 Microsoft)的引用。

In computer programming, particularly in the C, C++, and C# programming languages, a variable or object declared with the volatile keyword usually has special properties related to optimization and/or threading. Generally speaking, the volatile keyword is intended to prevent the (pseudo)compiler from applying any optimizations on the code that assume values of variables cannot change "on their own." (c) Wikipedia

http://en.wikipedia.org/wiki/Volatile_variable


Edit Wikipedia has fixed the nonsense about threading for C++, and now says

Operations on volatile variables are not atomic, nor do they establish a proper happens-before relationship for threading. This is specified in the relevant standards (C, C++, POSIX, WIN32), and volatile variables are not threadsafe in the vast majority of current implementations. Thus, the usage of volatile keyword as a portable synchronization mechanism is discouraged by many C/C++ groups.

For future reference, every claim in the comment thread below that volatile is useful for threading are either wholly incorrect extrapolations from other languages, or references to non-standard platform-specific extensions (specifically Microsoft).

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