Volatile is a qualifier used to define a data storage area (object, field, variable, parameter) that "can change on its own", thus disallowing some code generator optimizations. In some but not all languages that recognize this qualifier the access to such data is thread safe.
Volatile data are thread safe in
- C#
- Java (5 and above)
- Scala (depending on VM version)
Compilers ensure such semantics by emiting memory barrier instructions in the required order.
Volatile data are not thread safe in some implementations of
- C
- C++
- Java
In these languages, compilers are only required to refrain from particular code optimizations, especially those that would remove or reorder accesses to volatile data. This is still useful for some specialized purposes:
- Access to memory mapped devices
- Taming of atypical library constructs such as
longjmp
. - Some very careful thread synchronization protocols
(There is no relation between volatile data and non-volatile memory.)