Volatile (computer programming)
In computer programming, a variable is said to be volatile if its value can be read or modified asynchronously by something other than the current thread of execution.
The value of a
volatile variable may spontaneously change for reasons such as:sharing values with other threads;
sharing values with asynchronous signal handlers;
accessing hardware devices via memory-mapped I/O.
Support for these use cases varies considerably among the programming languages that have the
volatile keyword.Volatility can have implications regarding function calling conventions and how variables are stored, accessed and cached.
In C and C++
In C and C++,volatile is a type qualifier, like const, and is a part of a type. The behavior of the
volatile keyword in C and C++ is sometimes given in terms of suppressing optimizations of an optimizing compiler: 1- don't remove existing volatile reads and writes, 2- don't add new volatile reads and writes, and 3- don't reorder volatile reads and writes. However, this definition is only an approximation for the benefit of new learners, and this approximate definition should not be relied upon to write real production code.In C, and consequently C++, the
volatile keyword was intended to:- Allow access to memory-mapped I/O devices.
- Allow preserving values across a
longjmp. - Allow sharing values between signal handlers and the rest of the program in
volatilesig_atomic_tobjects.
longjmp in volatile objects, and the standards allow writing portable code that shares values between signal handlers and the rest of the code in volatile sig_atomic_t objects. Any other use of volatile keyword in C and C++ is inherently non-portable or incorrect. In particular, writing code with the volatile keyword for memory-mapped I/O devices is inherently non-portable and always requires deep knowledge of the specific target C/C++ implementation and platform.Multi-threading
It is a common misconception that thevolatile keyword is useful in portable multi-threading code in C and C++. The volatile keyword in C and C++ has never functioned as a useful, portable tool for any multi-threading scenario. Unlike the Java and C# programming languages, operations on volatile variables in C and C++ are not atomic, and operations on volatile variables do not have sufficient memory ordering guarantees. Most C and C++ compilers, linkers, and runtimes simply do not provide the necessary memory ordering guarantees to make the volatile keyword useful for any multi-threading scenario. Before the C11 and C++11 standards, programmers were forced to rely on guarantees from the individual implementations and platforms to write multi-threading code. With the modern C11 and C++11 standards, programmers can write portable multi-threading code using new portable constructs such as the std::atomic templates.Example of memory-mapped I/O in C
In this example, the code sets the value stored infoo to 0. It then starts to poll that value repeatedly until it changes to 255:static int foo;
void bar
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
However, the programmer may make
foo refer to another element of the computer system such as a hardware register of a device connected to the CPU which may change the value of foo while this code is running. Without the volatile keyword, an optimizing compiler will likely convert the code from the first sample with the read in the loop to the second sample without the read in the loop as part of the common loop-invariant code-motion optimization, and thus the code will likely never notice the change that it is waiting for.To prevent the compiler from doing this optimization, the
volatile keyword can be used:static volatile int foo;
void bar
The
volatile keyword prevents the compiler from moving the read out of the loop, and thus the code will notice the expected change to the variable foo.Optimization comparison in C
The following C programs, and accompanying assembler language excerpts, demonstrate how thevolatile keyword affects the compiler's output. The compiler in this case was GCC.While observing the assembly code, it is clearly visible that the code generated with
volatile objects is more verbose, making it longer so the nature of volatile objects can be fulfilled. The volatile keyword prevents the compiler from performing optimization on code involving volatile objects, thus ensuring that each volatile variable assignment and read has a corresponding memory access. Without the volatile keyword, the compiler knows a variable does not need to be reread from memory at each use, because there should not be any writes to its memory location from any other thread or process.Compiler defects
Unlike other language features of C and C++, thevolatile keyword is not well supported by most C/C++ implementations - even for portable uses according to the C and C++ standards. Most C/C++ implementations are buggy regarding the behavior of the volatile keyword. Programmers should take great care whenever using the volatile keyword in C and C++.In Java
In all modern versions of the Java programming language, thevolatile keyword gives the following guarantees:-
volatilereads and writes are atomic. In particular, reads and writes tolonganddoublefields will not tear. - There is a single global ordering of all
volatilereads and writes. In other words, avolatileread will read the current value, and allvolatilereads will agree on a single global order ofvolatilewrites. -
volatilereads and writes have "acquire" and "release" memory barrier semantics. In other words,volatileprovides guarantees about the relative order ofvolatileand non-volatilereads and writes. In other words,volatilebasically provides the same memory visibility guarantees as a Java synchronized block.
volatile into a useful multi-threading construct in Java. In particular, the typical double-checked locking algorithm with volatile works correctly in Java.Early versions of Java
Before Java version 5, the Java standard did not guarantee the relative ordering ofvolatile and non-volatile reads and writes. In other words, volatile did not have "acquire" and "release" memory barrier semantics. This greatly limited its use as a multi-threading construct. In particular, the typical double-checked locking algorithm with volatile did not work correctly.In C#
In C#,volatile ensures that code accessing the field is not subject to some thread-unsafe optimizations that may be performed by the compiler, the CLR, or by hardware. When a field is marked volatile, the compiler is instructed to generate a "memory barrier" or "fence" around it, which prevents instruction reordering or caching tied to the field. When reading a volatile field, the compiler generates an acquire-fence, which prevents other reads and writes to the field from being moved before the fence. When writing to a volatile field, the compiler generates a release-fence; this fence prevents other reads and writes to the field from being moved after the fence.Only the following types can be marked
volatile: all reference types, Single, Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Char, and all enumerated types with an underlying type of Byte, SByte, Int16, UInt16, Int32, or UInt32. Using the
volatile keyword does not support fields that are passed by reference or captured local variables; in these cases, Thread.VolatileRead and Thread.VolatileWrite must be used instead.In effect, these methods disable some optimizations usually performed by the C# compiler, the JIT compiler, or the CPU itself. The guarantees provided by
Thread.VolatileRead and Thread.VolatileWrite are a superset of the guarantees provided by the volatile keyword: instead of generating a "half fence", VolatileRead and VolatileWrite generate a "full fence" which prevent instruction reordering and caching of that field in both directions. These methods work as follows:- The
Thread.VolatileWritemethod forces the value in the field to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call toVolatileWriteand any later program-order loads and stores must occur after the call. - The
Thread.VolatileReadmethod forces the value in the field to be read from at the point of the call. In addition, any earlier program-order loads and stores must occur before the call toVolatileReadand any later program-order loads and stores must occur after the call.
Thread.VolatileRead and Thread.VolatileWrite methods generate a full fence by calling the Thread.MemoryBarrier method, which constructs a memory barrier that works in both directions. In addition to the motivations for using a full fence given above, one potential problem with the volatile keyword that is solved by using a full fence generated by Thread.MemoryBarrier is as follows: due to the asymmetric nature of half fences, a volatile field with a write instruction followed by a read instruction may still have the execution order swapped by the compiler. Because full fences are symmetric, this is not a problem when using Thread.MemoryBarrier.