In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, particularly in the
C,
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
C#, and
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s, the volatile
keyword
Keyword may refer to:
Computing
* Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic
* Index term, a term used as a keyword to documents in an information syst ...
indicates that a
value
Value or values may refer to:
Ethics and social
* Value (ethics) wherein said concept may be construed as treating actions themselves as abstract objects, associating value to them
** Values (Western philosophy) expands the notion of value beyo ...
may change between different accesses, even if it does not appear to be modified. This keyword prevents an
optimizing compiler
In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power cons ...
from optimizing away subsequent reads or writes and thus incorrectly reusing a stale value or omitting writes. Volatile values primarily arise in hardware access (
memory-mapped I/O
Memory-mapped I/O (MMIO) and port-mapped I/O (PMIO) are two complementary methods of performing input/output (I/O) between the central processing unit (CPU) and peripheral devices in a computer. An alternative approach is using dedicated I/O pro ...
), where reading from or writing to memory is used to communicate with
peripheral device
A peripheral or peripheral device is an auxiliary device used to put information into and get information out of a computer. The term ''peripheral device'' refers to all hardware components that are attached to a computer and are controlled by the ...
s, and in
threading, where a different thread may have modified a value.
Despite being a common keyword, the behavior of
volatile
differs significantly between programming languages, and is easily misunderstood. In C and C++, it is a
type qualifier In the C, C++, and D programming languages, a type qualifier is a keyword that is applied to a type, resulting in a ''qualified type.'' For example, const int is a qualified type representing a constant integer, while int is the corresponding u ...
, like
const
In some programming languages, const is a type qualifier (a keyword applied to a data type) that indicates that the data is read-only. While this can be used to declare constants, in the C family of languages differs from similar constructs i ...
, and is a property of the ''
type''. Furthermore, in C and C++ it does ''not'' work in most threading scenarios, and that use is discouraged. In Java and C#, it is a property of a
variable
Variable may refer to:
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
* Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
and indicates that the
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
to which the variable is bound may mutate, and is specifically intended for threading. In the
D programming language, there is a separate keyword
shared
for the threading usage, but no
volatile
keyword exists.
In C and C++
In C, and consequently C++, the
volatile
keyword was intended to
*allow access to
memory-mapped I/O
Memory-mapped I/O (MMIO) and port-mapped I/O (PMIO) are two complementary methods of performing input/output (I/O) between the central processing unit (CPU) and peripheral devices in a computer. An alternative approach is using dedicated I/O pro ...
devices
*allow uses of variables between
setjmp
setjmp.h is a header defined in the C standard library to provide "non-local jumps": control flow that deviates from the usual subroutine call and return sequence. The complementary functions setjmp and longjmp provide this functionality.
A t ...
and
longjmp
*allow uses of
sig_atomic_t
variables in signal handlers.
While intended by both C and C++, the C standards fail to express that the
volatile
semantics refer to the lvalue, not the referenced object. The respective defect report ''DR 476'' (to C11) is still under review with
C17.
Operations on
volatile
variables are not
atomic, nor do they establish a proper happens-before relationship for threading. This is specified in the relevant standards (C, C++,
POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
, WIN32),
and volatile variables are not threadsafe in the vast majority of current implementations. Thus, the usage of
volatile
keyword as a portable synchronization mechanism is discouraged by many C/C++ groups.
Example of memory-mapped I/O in C
In this example, the code sets the value stored in
foo
to
0
. It then starts to
poll
Poll, polled, or polling may refer to:
Figurative head counts
* Poll, a formal election
** Election verification exit poll, a survey taken to verify election counts
** Polling, voting to make decisions or determine opinions
** Polling places o ...
that value repeatedly until it changes to
255
:
static int foo;
void bar(void)
An
optimizing compiler
In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power cons ...
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
In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs ("pull the plug"). It may be intentional.
Overview
This differs from:
* ...
similar to this:
void bar_optimized(void)
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
In digital electronics, especially computing, hardware registers are circuits typically composed of flip flops, often with many characteristics similar to memory, such as:
* The ability to read or write multiple bits at a time, and
* Using an a ...
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)
With this modification the loop condition will not be optimized away, and the system will detect the change when it occurs.
Generally, there are
memory barrier
In computing, a memory barrier, also known as a membar, memory fence or fence instruction, is a type of barrier instruction that causes a central processing unit (CPU) or compiler to enforce an ordering constraint on memory operations issued be ...
operations available on platforms (which are exposed in C++11) that should be preferred instead of volatile as they allow the compiler to perform better optimization and more importantly they guarantee correct behaviour in multi-threaded scenarios; neither the C specification (before C11) nor the C++ specification (before C++11) specifies a multi-threaded memory model, so volatile may not behave deterministically across OSes/compilers/CPUs.
Optimization comparison in C
The following C programs, and accompanying assemblies, demonstrate how the
volatile
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.
C++11
According to the
C++11
C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
ISO Standard, the volatile keyword is only meant for use for hardware access; do not use it for inter-thread communication. For inter-thread communication, the standard library provides
std::atomic
templates.
In Java
The
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
also has the
volatile
keyword, but it is used for a somewhat different purpose. When applied to a field, the Java qualifier
volatile
provides the following guarantees:
*In all versions of Java, there is a global ordering on reads and writes of all volatile variables (this global ordering on volatiles is a partial order over the larger ''synchronization order'' (which is a total order over all ''synchronization actions'')). This implies that every
thread accessing a volatile field will read its current value before continuing, instead of (potentially) using a cached value. (However, there is no guarantee about the relative ordering of volatile reads and writes with regular reads and writes, meaning that it's generally not a useful threading construct.)
*In Java 5 or later, volatile reads and writes establish a
happens-before relationship, much like acquiring and releasing a mutex.
Using
volatile
may be faster than a
lock
Lock(s) may refer to:
Common meanings
*Lock and key, a mechanical device used to secure items of importance
*Lock (water navigation), a device for boats to transit between different levels of water, as in a canal
Arts and entertainment
* ''Lock ...
, but it will not work in some situations before Java 5. The range of situations in which volatile is effective was expanded in Java 5; in particular,
double-checked locking In software engineering, double-checked locking (also known as "double-checked locking optimization") is a software design pattern used to reduce the overhead of acquiring a lock by testing the locking criterion (the "lock hint") before acquiring th ...
now works 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, including those in other threads, 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
. (This excludes value
struct
In computer science, a record (also called a structure, struct, or compound data) is a basic data structure. Records in a database or spreadsheet are usually called "rows".
A record is a collection of '' fields'', possibly of different data typ ...
s, as well as the primitive types
Double
,
Int64
,
UInt64
and
Decimal
.)
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" (ie an acquire-fence only prevents instruction reordering and caching that comes before it),
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.VolatileWrite
method 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 to
VolatileWrite
and any later program-order loads and stores must occur after the call.
*The
Thread.VolatileRead
method 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 to
VolatileRead
and any later program-order loads and stores must occur after the call.
The
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
.
In Fortran
VOLATILE
is part of the
Fortran 2003 standard, although earlier version supported it as an extension. Making all variables
volatile
in a function is also useful finding
aliasing
In signal processing and related disciplines, aliasing is an effect that causes different signals to become indistinguishable (or ''aliases'' of one another) when sampled. It also often refers to the distortion or artifact that results when a ...
related bugs.
integer, volatile :: i ! When not defined volatile the following two lines of code are identical
write(*,*) i**2 ! Loads the variable i once from memory and multiplies that value times itself
write(*,*) i*i ! Loads the variable i twice from memory and multiplies those values
By always "drilling down" to memory of a VOLATILE, the Fortran compiler is precluded from reordering reads or writes to volatiles. This makes visible to other threads actions done in this thread, and vice versa.
Use of VOLATILE reduces and can even prevent optimization.
References
{{Reflist, 30em
External links
Ada Reference Manual C.6: Shared Variable ControlLinux kernel: volatile-considered-harmful
C (programming language)
Concurrency control
Variable (computer science)