Number series questions
June 12, 2012
Traversing a matrix in spiral order
June 12, 2012

volatile keyword in C / C++

What is the meaning of a volatile keyword in C & C++ language?
How can the variables be modified from outside the scope of current program?

Solution:

To understand volatile keyword, you may also want to have a look at the register keyword.

If a variable is expected to be read by the program more frequently, then it make sense to keep the variable in register and not read it from the memory every time it is used, register keyword allows us to suggest such optimization to the compiler. For example if an integer variable obj is declared as below:

register int obj;

then we are suggesting the compiler to keep variable obj in register.

This is only a suggestion, which compiler can ignore because of lack of availability of registers (what if you declare 100 variables as register, a CPU may not have that many registers) or size of the register being too small to hold the variable (for ex, if you declare an array as register).

If you do not declare a variable as register, but that variable is frequently read and not updated in the program, then compilers perform optimizations by keeping the variable in register (even if you have not declared them as register variable).

There may be a variable in your program which you know can be changed from outside the scope of the program (to know how a variable can be changed from outside, read below). For such variables, you do not want compiler to perform the optimizations on their own (by keeping variable in register) and read a fresh copy from the memory, every time that variable is accessed (even if it is read twice in the same program). volatile keyword does exactly that

volatile int obj; // Always read fresh copy from memory

Compiler will always read a fresh copy of obj from the memory, even if obj is available in the register and your program has not changed it. compiler will not assume anything.

Its good to use a volatile keyword for variables that are accessed in multiple threads.
Basically, without volatile,
either writing multithreaded programs becomes impossible,
or the compiler wastes vast optimization opportunities.

How a variable can be changed from outside the scope of program?
1. Global variables in a multi-threaded application:

Lets say you have a global variable flag and two function wait & notify as shown below:

    bool flag;
    void wait()
    {
        while( ! flag)
            sleep(1000);
    }
    void notify()
    {
        flag = true;
    }

If flag is false then a thread will wait unless some other thread calls the notify function and set the flag to true.

Now, inside the wait function, the sleep call does not modify the value of flag. So a compiler may think that since the value of flag cannot be changed, it will perform optimization by keeping the variable in register and not reading it fresh from memory for each loop. and the thread will wait for ever….

Caching variables in register is the most important and most common optimization performed by compilers, but in cases like abovve, it may put your code in trouble. So you can explicitly disable this caching by declaring variable as volatile

volatile bool flag;

2. Global variables modifiable by ISR (Interrupt Service Routine) outside the scope of program:

variables may be acting as a recipient of data from a port which is receiving data on the network. such variables will be updated when a data packet will come from the network and our program will not even know about it.. so such variables should also be defined as volatile.

In C++, you may also have volatile member functions (the way you have const member functions). Let me close this post here itself, I will consider volatile member functions in my next article. If you have any comment / Questions, please feel free to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *