In computer programming, volatile means that a value is prone to change over time, outside the control of some code. Volatility has implications within function calling conventions, and also impacts how variables are stored, accessed and cached.
In the C, C++, C#, and Java programming languages, the volatile keyword indicates that a value may change between different accesses, even if it does not appear to be modified. This keyword prevents an optimizing compiler 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), where reading from or writing to memory is used to communicate with peripheral devices, 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, like const
, 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 and indicates that the object 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 consequently C++, the volatile
keyword was intended to: [1]
setjmp
and longjmp
sig_atomic_t
variables in signal handlers.Since variables marked as volatile are prone to change outside the standard flow of code, the compiler has to perform every read and write to the variable as indicated by the code. Any access to volatile variables cannot be optimised away, e.g. by use of registers for storage of intermediate values.
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. [2]
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, WIN32), [1] 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. [3] [4] [5]
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
:
staticintfoo;voidbar(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:
voidbar_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:
staticvolatileintfoo;voidbar(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.
Generally, there are memory barrier 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. [6]
The following C programs, and accompanying assembler language excerpts, 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.
Assembly comparison | |
---|---|
Without volatile keyword | With volatile keyword |
#include<stdio.h>intmain(){/* These variables will never be created on stack*/inta=10,b=100,c=0,d=0;/* "printf" will be called with arguments "%d" and 110 (the compiler computes the sum of a+b), hence no overhead of performing addition at run-time */printf("%d",a+b);/* This code will be removed via optimization, but the impact of 'c' and 'd' becoming 100 can be seen while calling "printf" */a=b;c=b;d=b;/* Compiler will generate code where printf is called with arguments "%d" and 200 */printf("%d",c+d);return0;} | #include<stdio.h>intmain(){volatileinta=10,b=100,c=0,d=0;printf("%d",a+b);a=b;c=b;d=b;printf("%d",c+d);return0;} |
gcc -S -O3 -masm=intel noVolatileVar.c -o without.s | gcc -S -O3 -masm=intel VolatileVar.c -o with.s |
.file"noVolatileVar.c".intel_syntaxnoprefix.section.rodata.str1.1,"aMS",@progbits,1.LC0:.string"%d".section.text.startup,"ax",@progbits.p2align4,,15.globlmain.typemain,@functionmain:.LFB11:.cfi_startprocsubrsp,8.cfi_def_cfa_offset16movesi,110movedi,OFFSETFLAT:.LC0xoreax,eaxcallprintfmovesi,200movedi,OFFSETFLAT:.LC0xoreax,eaxcallprintfxoreax,eaxaddrsp,8.cfi_def_cfa_offset8ret.cfi_endproc.LFE11:.sizemain,.-main.ident"GCC: (GNU) 4.8.2".section.note.GNU-stack,"",@progbits | .file"VolatileVar.c".intel_syntaxnoprefix.section.rodata.str1.1,"aMS",@progbits,1.LC0:.string"%d".section.text.startup,"ax",@progbits.p2align4,,15.globlmain.typemain,@functionmain:.LFB11:.cfi_startprocsubrsp,24.cfi_def_cfa_offset32movedi,OFFSETFLAT:.LC0movDWORDPTR[rsp],10movDWORDPTR[rsp+4],100movDWORDPTR[rsp+8],0movDWORDPTR[rsp+12],0movesi,DWORDPTR[rsp]moveax,DWORDPTR[rsp+4]addesi,eaxxoreax,eaxcallprintfmoveax,DWORDPTR[rsp+4]movedi,OFFSETFLAT:.LC0movDWORDPTR[rsp],eaxmoveax,DWORDPTR[rsp+4]movDWORDPTR[rsp+8],eaxmoveax,DWORDPTR[rsp+4]movDWORDPTR[rsp+12],eaxmovesi,DWORDPTR[rsp+8]moveax,DWORDPTR[rsp+12]addesi,eaxxoreax,eaxcallprintfxoreax,eaxaddrsp,24.cfi_def_cfa_offset8ret.cfi_endproc.LFE11:.sizemain,.-main.ident"GCC: (GNU) 4.8.2".section.note.GNU-stack,"",@progbits |
According to the C++11 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<T>
templates. [7]
The Java programming language 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:
Using volatile
may be faster than a lock, but it will not work in some situations before Java 5. [10] The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly. [11]
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. [12]
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
. [13] (This excludes value structs, 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. [12]
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. [12] These methods work as follows: [14]
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.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
. [12]
VOLATILE
is part of the Fortran 2003 standard, [15] although earlier version supported it as an extension. Making all variables volatile
in a function is also useful finding aliasing related bugs.
integer,volatile::i! When not defined volatile the following two lines of code are identicalwrite(*,*)i**2! Loads the variable i once from memory and multiplies that value times itselfwrite(*,*)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. [16]
Use of VOLATILE reduces and can even prevent optimization. [17]
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 consumption.
Java and C++ are two prominent object-oriented programming languages. By many language popularity metrics, the two languages have dominated object-oriented and high-performance software development for much of the 21st century, and are often directly compared and contrasted. Java's syntax was based on C/C++.
Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction. There are various strategies for making thread-safe data structures.
In software engineering, double-checked locking is a software design pattern used to reduce the overhead of acquiring a lock by testing the locking criterion before acquiring the lock. Locking occurs only if the locking criterion check indicates that locking is required.
A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of the possible behaviors is undesirable.
This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.
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 before and after the barrier instruction. This typically means that operations issued prior to the barrier are guaranteed to be performed before operations issued after the barrier.
The syntax of Java is the set of rules defining how a Java program is written and interpreted.
In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time on each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.
In computer programming, thread-local storage (TLS) is a memory management method that uses static or global memory local to a thread.
In the Java programming language, the final
keyword is used in several contexts to define an entity that can only be assigned once.
In compiler optimization, escape analysis is a method for determining the dynamic scope of pointers – where in the program a pointer can be accessed. It is related to pointer analysis and shape analysis.
The Java memory model describes how threads in the Java programming language interact through memory. Together with the description of single-threaded execution of code, the memory model provides the semantics of the Java programming language.
The Java programming language and the Java virtual machine (JVM) is designed to support concurrent programming. All execution takes place in the context of threads. Objects and resources can be accessed by many separate threads. Each thread has its own path of execution, but can potentially access any object in the program. The programmer must ensure read and write access to objects is properly coordinated between threads. Thread synchronization ensures that objects are modified by only one thread at a time and prevents threads from accessing partially updated objects during modification by another thread. The Java language has built-in constructs to support this coordination.
Memory ordering describes the order of accesses to computer memory by a CPU. The term can refer either to the memory ordering generated by the compiler during compile time, or to the memory ordering generated by a CPU during runtime.
This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.
In computing, a memory model describes the interactions of threads through memory and their shared use of the data.
In computer programming, a constant is a value that is not altered by the program during normal execution, i.e., the value is constant. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable.
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 unqualified type, simply an integer. In D these are known as type constructors, by analogy with constructors in object-oriented programming.
{{cite web}}
: CS1 maint: bot: original URL status unknown (link)