BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 12 definitions for Volatile.

Volatile variable

Print-Friendly
About 1 pages (390 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

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 piece of C source code demonstrates 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

View More Summaries on Volatile variable
 
Ask any question on Volatile variable and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Volatile variable from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy