Volatile关键字

 

Volatile关键字

·         The compiler assumes that, at any point in the program, avolatile variable can be accessed by an unknown process that uses or modifies its value. Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of avolatile variable must be generated even if it appears to have no effect.

If volatile is used alone, int is assumed. Thevolatile type specifier can be used to provide reliable access to special memory locations. Usevolatile with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable asvolatile for its lifetime, or you can cast a single reference to bevolatile.

 

·         直接存取原始内存地址

           能够防止编译器优化。

            编译器每次用到这个变量的值的时候都会直接从内存中读取而不是读取寄存器中的备份。通过反汇编可能会发现一些问题(待续).http://www.dutor.net/index.php/2010/08/volatile-const/


原文地址:https://www.cnblogs.com/johnpher/p/2570621.html