为什么我们在c++中使用volatile关键字?(复制)

时间:2020-12-15 20:56:50

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

3 个解决方案

#1


846  

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 simply 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 (if it's not loaded on a register) and compare it with 100, each time which obviously is a little bit slow.)

当这个程序被编译,编译器会优化代码,如果发现程序永远不会让任何试图改变some_int的价值,所以它可能会优化while循环通过改变从(some_int = = 100),简单而(真正的),这样可以快速执行(因为while循环的条件似乎是真的总是)。(如果编译器没有对它进行优化,那么它必须获取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; but it's how you've designed it. In that case, compiler's optimization would not produce the desired result!

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

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,

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

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可以是任何东西。也许这个星球外的外星生物叫program。也许一些灯光,一些中断,火山等等可以让我变异。也许吧。你永远不知道谁会改变我!所以啊,无知的你,别再扮演无所不知的上帝了,别再碰我在场的密码了。没事吧?”

Well, that is how volatile prevents compiler from optimizing code. Now google it 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 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?

使结构体具有不稳定性是否会使其所有成员都具有不稳定性?

#2


8  

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

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

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

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

#3


-8  

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

volatile关键字是一种类型限定符,用于声明可以通过操作系统、硬件或并发执行的线程等方式在程序中修改对象。

volatile declarator ;

挥发性说明符;

#1


846  

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 simply 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 (if it's not loaded on a register) and compare it with 100, each time which obviously is a little bit slow.)

当这个程序被编译,编译器会优化代码,如果发现程序永远不会让任何试图改变some_int的价值,所以它可能会优化while循环通过改变从(some_int = = 100),简单而(真正的),这样可以快速执行(因为while循环的条件似乎是真的总是)。(如果编译器没有对它进行优化,那么它必须获取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; but it's how you've designed it. In that case, compiler's optimization would not produce the desired result!

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

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,

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

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可以是任何东西。也许这个星球外的外星生物叫program。也许一些灯光,一些中断,火山等等可以让我变异。也许吧。你永远不知道谁会改变我!所以啊,无知的你,别再扮演无所不知的上帝了,别再碰我在场的密码了。没事吧?”

Well, that is how volatile prevents compiler from optimizing code. Now google it 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 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?

使结构体具有不稳定性是否会使其所有成员都具有不稳定性?

#2


8  

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

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

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

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

#3


-8  

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

volatile关键字是一种类型限定符,用于声明可以通过操作系统、硬件或并发执行的线程等方式在程序中修改对象。

volatile declarator ;

挥发性说明符;