Volatile variable

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

In computer programming, particularly in the C, C++, and C# programming languages, a variable or object declared with the volatile keyword may be modified externally from the declaring object. 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 [1]. Note that operations on a volatile variable in C and C++ are not guaranteed to be atomic.

Use

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:

static int foo;
 
void bar (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:

void bar_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:

static volatile int foo;
 
void bar (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.

The volatile keyword may not be implemented in all systems, and using it as a threading or synchronization primitive is not guaranteed to work in C, C++, or Java versions 1 to 1.4. In C, and consequently C++, the keyword was intended to:[citation needed]

  • allow access to memory mapped devices
  • allow uses of variables between setjmp and longjmp
  • allow uses of variables in signal handlers

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

Optimization comparison in C

The following C programs, and accompanying disassemblies, demonstrate how the volatile keyword affects the compiler's output. The compiler in this case was GCC.

In Java

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 volatile guarantees that:

  1. (In all versions of Java) Every thread accessing the field will read its current value before continuing, instead of (potentially) using a cached value.
  2. (In Java 5 or later) Statements accessing the variable will be executed in the order they are written.[2]

Using volatile may be faster than a lock, but it will not work in all situations. The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.[3]

References

  1. Sams Teach Yourself C in 24 Hours (2nd Edition); 2nd edition (2000);Authors: Tony Zhang, John Southmayd; Chapter 14: The Volatile Modifier; Sams_Publishing ; ISBN-13:978-0672318610
  2. "A field may be declared volatile[...]" "The Java Language Specification, 2nd Edition, Section 8.3.1.4: volatile Fields". Sun Microsystems. 2000. http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#36930. Retrieved 2009-05-25. 
  3. Neil Coffey. "Double-checked Locking (DCL) and how to fix it". Javamex. http://www.javamex.com/tutorials/double_checked_locking_fixing.shtml. Retrieved 2009-09-19. 

External links

cs:Volatile

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...