volatile declarator
The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something other than statements, such as the operating system, the hardware, or a concurrently executing thread.
The following example declares a volatile integer nVint
whose
value can be modified by external processes:
int volatile nVint;
Objects declared as volatile are not used in optimizations because their value can change at any time. The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment.
One use of the volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.
msdn上面的说明
我来粗略翻译一下吧:
volatile说明
关键字volatile是一种用于声明且限定只能被非程序中的语句所修改变量的类型,例如操作系统,硬件或者多线程.
下面的例子中声明了一个可以通过外部进程改变的volatile整型变量nWint:
int volatile nWint;
所有的变量都定义成volatile类型在应用中并不是最佳的,因为他们的值随时能更改.即使此前的指令从同一变量中返回一个值,但系统只读取这个volatile变量在被调用时当前的值.
利用volatile类型的限制可以为不同进程提供内存区域的访问,例如中断操作