为什么在C ++中使用volatile关键字? [英] Why do we use volatile keyword in C++?

查看:106
本文介绍了为什么在C ++中使用volatile关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:


我从来没有使用它,但我不知道为什么人们使用它?它究竟做了什么?我在搜索论坛,我发现它只有C#或Java主题。

考虑这个代码,

  int some_int = 100; 

while(some_int == 100)
{
//您的代码
}

当这个程序被编译时,编译器可以优化这个代码,如果它发现程序从不曾经尝试改变 some_int ,因此可能会试图通过从改变 while(some_int = = 100)简单地 while(true),以便执行速度快(因为 code>循环似乎 true always)。 (如果编译器没有优化它,那么它必须获取 some_int (如果它没有加载到寄存器)的值,并将其与100, )



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



因此,为了确保所需的结果,从优化 while 循环。这是 volatile 关键字扮演它的角色。所有你需要做的是这个,

  volatile int some_int = 100; //现在注意'volatile'限定词! 






换句话说,



volatile 告诉编译器


编译器,我很不稳定,你
知道,我可以改变一些你甚至不知道的XYZ
那个
XYZ可以也许有一些
外星人在这个星球之外称为
程序。也许一些照明,一些
形式的中断,火山等可以
mutate我。
将改变我!所以你
无知,停止玩一个知道的
神,不敢触摸代码
我在场。 ?


那么, volatile 会阻止编译器优化代码。






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



strong>
因为对象的值可能
被改变,意味着无法通过
实现检测[...]


相关主题:



使一个struct变量使它的所有成员都变动吗?


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

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.

解决方案

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.)

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 it's role. 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,

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

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

这篇关于为什么在C ++中使用volatile关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆