您的位置:首页 > 其它

volatile的用法

2016-02-29 20:57 190 查看
volatile是一个关于优化的关键字,目的是告诉编译器每次都要去读变量本身的值,而不是简单的将其优化为一个已知的值。

Volatile const int a = 10;

表示a是一个整形的常量,代码中不应该去修改它,但是它可以被外部修改,比如中断等

lcl:

volatile是一个关于优化的关键字,目的是告诉编译器每次都要去读变量本身的值,而不是简单地将其优化为一个已知的值。

volatile const int a = 10;

表示a 是一个整形的常量,代码中不应该去修改它,但是它可以被外部修改,比如中断等

比较易于理解的解释:

“volatile的本意是“易变的”,volatile关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改,比如操作系统、硬件或者其它线程等。遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进行优化,从而可以提供对特殊地址的稳定访问。

当要求使用volatile
声明的变量的值的时候,系统总是重新从它所在的内存读取数据,即使它前面的指令刚刚从该处读取过数据。而且读取的数据立刻被寄存”

参考网站:http://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c
http://freestyler.pixnet.net/blog/post/23872864-c-c%2B%2B%E4%B8%AD%E7%9A%84volatile%E4%BD%BF%E7%94%A8%E6%99%82%E6%A9%9F%3F http://blog.csdn.net/wuliming_sc/article/details/3717017 http://stackoverflow.com/questions/16259939/const-volatile-register-volatile-static-volatile-in-c
附录:

Consider this code,

int some_int =
100;

while(some_int ==
100)

{

//yourcode

}

When this program getscompiled, 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
while loop by changing it from
while(some_int== 100) to simply
while(true) so that the execution could be fast (sincethe condition in
while loop appears to be
true always).
(if the compiler doesn't optimizeit, then it has to fetch the value of some_int (if it's not loaded on a register) andcompare it with 100, each time which
obviously is a little bit slow.)

However, sometimes,optimization (of some parts of your program) may be
undesirable, becauseit may be that someone else is changing the value of
some_int from
outside the program which compiler isnot aware of, since it can't see it; but it's how you've designed it. Inthat case, compiler's optimization would
not produce the desired result!

So, to ensure thedesired 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 dois this,

volatile
intsome_int = 100;
//notethe 'volatile' qualifier now!

In others words Iwould explain this as follows:

volatile tells the compiler that,

"Heycompiler, I'm volatile and, you know, I can be changed by some XYZ that you'renot even aware of. That XYZ could be anything. Maybe some alien outside thisplanet 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 youignorant, stop playing an all-knowing god, and don't dare touch the code whereI'm present. Okay?"

Well, that is how volatile prevents compiler from optimizing code. Nowgoogle it to see some sample examples.

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

[..] volatile is a hint to the implementationto
avoid aggressive optimization involving the object
because the valueof the object might be changed by means undetectable by an implementation.[...]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: