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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4437527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:24:50  来源:igfitidea点击:

Why do we use volatile keyword?

c++volatilecompiler-optimization

提问by Nawaz

Possible Duplicate:
C++: When Has The volatile Keyword Ever Helped You?

可能的重复:
C++:volatile 关键字何时帮助过您?

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.

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

回答by Nawaz

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 neverever makes any attempt to change the value of some_int, so it may be tempted to optimize the whileloop by changing it from while(some_int == 100)to somethingwhich is equivalent to while(true)so that the execution could be fast (since the condition in whileloop appears to be truealways). (if the compiler doesn't optimize it, then it has to fetch the value of some_intand compare it with 100, in each iteration which obviously is a little bit slow.)

当此程序被编译,编译器优化的代码,如果它认为,该方案从来没有能做出任何试图改变的价值some_int,所以它可能会倾向于优化while通过更改其循环while(some_int == 100)的东西,相当于while(true)使执行速度可能很快(因为while循环中的条件似乎true总是如此)。(如果编译器没有优化它,那么它必须some_int在每次迭代中获取 的值并将其与 100 进行比较,这显然有点慢。)

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_intfrom 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 notproduce the desired result!

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

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

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

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


In other words, I would explain this as follows:

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

volatiletells the compiler that,

volatile告诉编译器,

"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?"

“嘿编译器,我是不稳定的,你知道,我可以被一些你甚至不知道的 XYZ 改变。那个 XYZ 可以是任何东西。也许这个星球之外的某个外星人叫做程序。也许一些闪电,某种形式打断、火山等可以让我变异。也许。你永远不知道谁会改变我!所以你这个无知的人,别再扮演无所不知的神了,不要在我在场的地方碰代码。好吧?”

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

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



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

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

[..] volatile is a hint to the implementation to avoid aggressive optimization involving the objectbecause the value of the object might be changed by means undetectable by an implementation.[...]

[..] volatile 是对实现的提示,以避免涉及对象的激进优化,因为对象的值可能会通过实现无法检测的方式更改。[...]

Related topic:

相关主题:

Does making a struct volatile make all its members volatile?

使 struct volatile 是否会使其所有成员都变得 volatile?

回答by Ivan

In computer programming, particularly in the C, C++, and C# programming languages, a variable or object declared with the volatilekeyword usually has special properties related to optimization and/or threading. Generally speaking, the volatilekeyword 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

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

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

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