Volatile variable

Volatile variable

In computer programming, a variable or object declared with the volatile keyword may be modified externally from the declaring object. For example, a variable that might be concurrently modified by multiple threads may be declared volatile. Variables declared to be volatile will not be optimized by the compiler because the compiler must assume that their values can change at any time. Note that operations on a volatile variable are still not guaranteed to be atomic.

What can happen if volatile is not used?

The following pieces of C source code demonstrate the use of the volatile keyword.

static int foo; void bar(void) { foo = 0; while (foo != 255) continue; }

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. An optimizing compiler will notice that no other code can possibly change the value stored in foo, and therefore assume that it will remain equal to 0 at all times. The compiler will then replace the function body with an infinite loop, similar to this: void bar_optimized(void) { foo = 0; while (TRUE) continue; }

However, foo might represent a location that can be changed by other elements of the computer system. For example, the variable may be modified by another thread or process via shared memory. It could even be a hardware register of a device connected to the CPU. The value stored there could change at any time. The above code would never detect such a change; without the volatile keyword, the compiler assumes the current program is the only part of the system that could cause the value to change. (This is by far the most common situation.)

To prevent the compiler from modifying code in this way, the volatile keyword is used in the following manner:

static volatile int foo; void bar(void) { foo = 0; while (foo != 255) continue; }

With this modification the loop condition will not be optimized away, and the CPU will detect the change when it occurs.

For an example of the use of volatile in context, see Busy waiting.

External links

* [http://www.programmersheaven.com/articles/pathak/article1.htm Use of volatile at Programmer's Heaven]
* [http://msdn2.microsoft.com/en-us/library/12a04hfd.aspx volatile keyword in Visual C++]
* [http://kernel.org/doc/Documentation/volatile-considered-harmful.txt Why the "volatile" type class should not be used (Linux kernel documentation)]
* [http://softwareblogs.intel.com/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/ Volatile: Almost Useless for Multi-Threaded Programming (Intel Software Network)]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Volatile — kommt aus dem englischen Sprachraum und bedeutet dort etwa: veränderlich, beweglich, flüchtig. Das Wort wird als Fachausdruck auch im deutschen Sprachraum verwendet: Im Aktienhandel für veränderliche Werte oder als Maß des Risikos einer Aktie,… …   Deutsch Wikipedia

  • Volatile (Informatik) — Volatile ist ein Zusatz bei der Deklaration von Variablen in Programmiersprachen wie C, C++, Java oder C#. In C und C++ wird durch diesen Typqualifikator spezifiziert, dass sich der Wert der Variablen außerhalb des aktuellen Programmkontextes… …   Deutsch Wikipedia

  • volatile — I adjective active, animated, brief, brisk, buoyant, capricious, changeable, cometary, deciduous, desultory, effervescent, elastic, elusive, ephemeral, erratic, evanescent, evaporable, excitable, explosive, fickle, fleeting, flighty, full of… …   Law dictionary

  • volatile — effervescent, buoyant, expansive, resilient, *elastic Analogous words: unstable, mercurial, *inconstant, fickle, capricious: light minded, frivolous, flippant, flighty (see corresponding nouns at LIGHTNESS): variable, *changeable, protean …   New Dictionary of Synonyms

  • volatile — [adj] explosive, changeable airy, buoyant, capricious, effervescent, elastic, elusive, ephemeral, erratic, expansive, fickle, fleeting, flighty, flippant, frivolous, fugacious, fugitive, gaseous, gay, giddy, impermanent, imponderable,… …   New thesaurus

  • variable — I adjective aberrant, alterable, capricious, changeable, changeful, erratic, faithless, fanciful, fast and loose, fickle, fitful, fluctuating, inconstant, irregular, irresponsible, mercurial, modifiable, oscillating, protean, shifting, spasmodic …   Law dictionary

  • variable — [adj] changing, changeable capricious, changeful, fickle, fitful, flexible, fluctuating, fluid, iffy*, inconstant, irregular, mercurial, mobile, mutable, protean, shifting, shifty, slippery*, spasmodic, temperamental, ticklish, uncertain,… …   New thesaurus

  • volatile — adjective 1) a volatile personality Syn: unpredictable, changeable, variable, inconstant, inconsistent, erratic, irregular, unstable, turbulent, blowing hot and cold, varying, shifting, fluctuating, fluid, mutable; mercurial …   Thesaurus of popular words

  • Volatile — Oiseau Pour les articles homonymes, voir oiseau (homonymie) …   Wikipédia en Français

  • Variable velocity — Velocity Ve*loc i*ty, n.; pl. {Velocities}. [L. velocitas, from velox, ocis, swift, quick; perhaps akin to v?lare to fly (see {Volatile}): cf. F. v[ e]locit[ e].] [1913 Webster] 1. Quickness of motion; swiftness; speed; celerity; rapidity; as,… …   The Collaborative International Dictionary of English

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”