From: http://en.wikipedia.org/wiki/Volatile_variable#In_C_and_C.2B.2B
In C and C++
In C, and consequently C++, the volatile
keyword was intended to[ 1]
- allow access to memory mapped devices
- allow uses of variables between
setjmp
andlongjmp
- allow uses of sig_atomic_t variables in signal handlers
Operations on volatile
variables are not atomic , nor do they establish a proper happens-before relationship for threading. This is according to the relevant standards (C, C++, POSIX, WIN32), and this is the matter of fact for the vast majority of current implementations. The volatile
keyword is basically worthless as a portable threading construct.[ 2] [ 3] [ 4] [ 5] [ 6]
Example of MMIO In C
In this example, the code sets the value stored in foo
to 0
. It then starts to poll that value repeatedly until it changes to 255
:
static int foo;
void bar( void ) {
foo = 0 ;
while ( foo != 255)
;
}
An optimizing compiler will notice that no other code can possibly change the value stored in foo
, and will assume that it will remain equal to 0
at all times. The compiler will therefore replace the function body with an infinite loop similar to this:
void bar_optimized( void ) {
foo = 0 ;
while ( true )
;
}
However, foo
might represent a location that can be changed by other elements of the computer system at any time, such as a hardware register of a device connected to the CPU . The above code would never detect such a change; without the volatile
keyword, the compiler assumes that the current program is the only part of the system that could change the value (which is by far the most common situation).
To prevent the compiler from optimizing code as above, the volatile
keyword is used:
static volatile int foo;
void bar ( void ) {
foo = 0 ;
while ( foo != 255)
;
}
With this modification the loop condition will not be optimized away, and the system will detect the change when it occurs.
Optimization comparison in C
The following C programs, and accompanying disassemblies, demonstrate how the volatile
keyword affects the compiler's output. The compiler in this case was GCC .
[hide ] Disassembly comparison | ||||||||
---|---|---|---|---|---|---|---|---|
|