Jump to content

Volatile (computer programming)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 82.33.61.156 (talk) at 18:45, 14 June 2009 (Memorry -> Memory). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, particularly in the 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. Note that operations on a volatile variable in C and C++ are not guaranteed to be atomic.

The Java programming language also has the volatile keyword.[1]

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

Without volatile With volatile
#include <stdio.h>

int main() {
    int a = 10, b = 100, c = 0, d = 0;

    printf("%d", a + b);

    a = b;
    c = b;
    d = b;

    printf("%d", c + d);

    return 0;
}
#include <stdio.h>

int main() {
    volatile int a = 10, b = 100, c = 0, d = 0;

    printf("%d", a + b);

    a = b;
    c = b;
    d = b;

    printf("%d", c + d);

    return 0;
}
gcc -O3 -S without.c -o without.s gcc -O3 -S with.c -o with.s
    .file   "without.c"
    .section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
    .string "%d"
    .text
    .p2align 4,,15
.globl main
    .type   main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $20, %esp
    movl    $110, 4(%esp)





    movl    $.LC0, (%esp)


    call    printf
    movl    $200, 4(%esp)







    movl    $.LC0, (%esp)


    call    printf
    addl    $20, %esp
    xorl    %eax, %eax
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"
	.file   "with.c"
	.section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string "%d"
	.text
	.p2align 4,,15
.globl main
	.type   main, @function
main:
	leal    4(%esp), %ecx
	andl    $-16, %esp
	pushl   -4(%ecx)
	pushl   %ebp
	movl    %esp, %ebp
	pushl   %ecx
	subl    $36, %esp
	movl    $10, -8(%ebp)
	movl    $100, -12(%ebp)
	movl    $0, -16(%ebp)
	movl    $0, -20(%ebp)
	movl    -8(%ebp), %edx
	movl    -12(%ebp), %eax
	movl    $.LC0, (%esp)
	addl    %edx, %eax
	movl    %eax, 4(%esp)
	call    printf
	movl    -12(%ebp), %eax
	movl    %eax, -8(%ebp)
	movl    -12(%ebp), %eax
	movl    %eax, -16(%ebp)
	movl    -12(%ebp), %eax
	movl    %eax, -20(%ebp)
	movl    -16(%ebp), %edx
	movl    -20(%ebp), %eax
	movl    $.LC0, (%esp)
	addl    %edx, %eax
	movl    %eax, 4(%esp)
	call    printf
	addl    $36, %esp
	xorl    %eax, %eax
	popl    %ecx
	popl    %ebp
	leal    -4(%ecx), %esp
	ret
	.size   main, .-main
	.ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

References

  1. ^ "A field may be declared volatile[...]" "The Java Language Specification, 2nd Edition, Section 8.3.1.4: volatile Fields". Sun Microsystems. 2000. Retrieved 2009-05-25.