为什么我们使用volatile关键字? [重复]

本文翻译自:Why do we use volatile keyword? [duplicate]

Possible Duplicate: 可能重复:
C++: When Has The volatile Keyword Ever Helped You? C ++:什么时候挥动的关键字帮助了你?

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主题。


#1楼

参考:https://stackoom.com/question/IcP1/为什么我们使用volatile关键字-重复


#2楼

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

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


#3楼

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). 当编译该程序时,编译器可以优化此代码,如果它发现程序从未尝试更改some_int的值,那么可能很想通过将while更改为while(some_int == 100)来优化while循环while(some_int == 100)对等于while(true) 某些东西 ,以便执行可以很快(因为while循环中的条件似乎总是为true )。 (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.) (如果编译器没有优化它,那么它必须获取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_int from outside the program which compiler is not aware of , since it can't see it; 但是,有时,优化(程序的某些部分)可能是不合需要的 ,因为可能是其他人正在从编译器不知道的程序外部更改some_int的值,因为它无法看到它; 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. 因此,为了确保获得所需的结果,您需要以某种方式阻止编译器优化while循环。 That is where the volatile keyword plays its role. 这就是volatile关键字发挥作用的地方。 All you need to do is this, 你需要做的就是这个,

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

In others words I would explain this as follows: 换句话说,我会解释如下:

volatile tells 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 lighting, 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 volatile prevents compiler from optimizing code. 好吧,那就是volatile如何阻止编译器优化代码。 Now search the web to see some sample examples. 现在搜索网络以查看一些示例示例。


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 object because 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? 使结构变为volatile会使其所有成员变得不稳定吗?

猜你喜欢

转载自blog.csdn.net/p15097962069/article/details/107660708