为什么我们使用volatile关键字? [英] Why do we use volatile keyword?

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

问题描述


可能重复:

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

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.

推荐答案

请考虑以下代码,

int some_int = 100;

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

编译该程序时,如果编译器发现程序从不曾经尝试更改<$的值,则编译器可以优化该代码。 c $ c> some_int ,因此可能很想通过将 while 循环从 while(some_int = = 100) something ,它等效于 while(true),因此执行速度可能很快(因为 while 循环中的条件似乎总是 true )。 (如果编译器未对其进行优化,则它必须获取 some_int 的值并将其与100进行比较,这在每次迭代中显然是一点点

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

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

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!

因此,为了确保预期的结果,您需要以某种方式停止编译器通过优化 while 循环。这就是 volatile 关键字起作用的地方。您所需要做的就是这个,

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 告诉编译器,


嘿,编译器,我易失,您
知道,我可以用您甚至不知道的XYZ
来更改。那个
XYZ可能是任何事情,也许这颗行星之外的某些
外星人称为
程序,也许某些闪电,某些
形式的中断,火山等可能会使
使我变异。也许。谁
会改变我!所以,您
一无所知,不要再扮演一个全知的
神,并且不要敢碰我现在所在的代码
。好吧?

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

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

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

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

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.[...]

相关主题:

将结构设为易失性会使其所有成员易失吗?

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

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