Volatile (computer programming): Difference between revisions

Content deleted Content added
+wikilink
Lede: increase in scope for article
Line 1:
{{Short description|A keyword used in some programming languages to tag variables}}
{{Lowercase title}}
In [[computer programming]], particularly'''volatile''' inmeans that a value is prone to change over time, outside the control of some code. Volatility has implications within function [[calling convention]]s, and also impacts how variables are stored, accessed and cached.

In the [[C (programming language)|C]], [[C++]], [[C Sharp (programming language)|C#]], and [[Java (programming language)|Java]] [[programming language]]s, the '''volatile''' [[keyword (computer programming)|keyword]] indicates that a [[Value (computer science)|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 device]]s, and in [[thread (computing)|threading]], where a different thread may have modified a value.
 
Despite being a common keyword, the behavior of <code>volatile</code> differs significantly between programming languages, and is easily misunderstood. In C and C++, it is a [[type qualifier]], like <code>[[const (computer programming)|const]]</code>, and is a property of the ''[[data type|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 (computer science)|variable]] and indicates that the [[object (computer science)|object]] to which the variable is bound may mutate, and is specifically intended for threading. In the [[D (programming language)|D]] programming language, there is a separate keyword <code>shared</code> for the threading usage, but no <code>volatile</code> keyword exists.