Saturday 21 March 2015

Volatile Keyword


In this post I am going to describe why we use volatile keyword.

I am assuming you have a basic knowledge of compiler. Whenever we write a code and compile it. Compiler optimizes our code according to some basic fundamentals.

Like for eg. If in our code we have a constant variable whose value cannot be modified and we have a loop checking this constant variable with another constant. Means if compiler thinks that none part of our code is changing the value of the variable than it can optimize the comparisons.

Ex:
I am not writing the complete code. I am writing only the part which considers the above fact.

const int a=10;

while(a<20){    /// focus on this line
      some task;
}
Now At compile time compiler will check that variable a is constant and its value cannot be changed. And in the while loop it determines that 20 is also constant so it is also not changed. So it evalutes the result of a<20 and put it in the while condition.

Then compiler will put either true or false there to optimize. So that at each time it iterate through the loop it does not have to compare the variable.

For ex: After optimization code looks like this.

const int a=10;

while(true){   /// focus on this line
      some task;
}

But sometime this optimization affect the code in the wrong way.
Like : If we have a share variable between two files or two program then we don't want compiler to do any optimization with those variables. 

In this case we can define those variables to volatile. So compiler will not apply any kind of optimization on these variables.

Scenario 1: Variable with Volatile Keyword :

Ex:  we have a variable called a which is shared in two threads thread1 and thread2.

Volatile Keyword


Explanation: We have a volatile variable a=1 in thread 1 and it is shared. When thread 1 runs it goes in to while loop and check for a and goes in to the loop and do the task and switch to thread 2. Now thread 2 also checks a=1 goes in to loop and make a=0 and then again switch to thread 1 at line 8.

Now it checks the value of a which is equal to 0 and come out of the loop.


Scenario 2: Variable without Volatile Keyword.

Now consider the scenario where value of a is optimized. Means without volatile.

In thread 1;
int a=1;

some task;
while(a){  /// this is replaced by while(1)   /// optimized by compiler    ///// line 1
    do some task;  
   switch to thread2;
   //// line8
}


In thread 2:

while(a){
   do some task;
    a=0;
   switch to thread 1;
}


Explanation: We have a variable a=1 in thread 1 and it is shared. When thread 1 runs it check for a and goes in to the loop and do the task and switch to thread 2. Now thread 2 also checks if a!=0 and goes in to loop and make a=0 and then again switch to thread 1 at line 5.

Due to while(1).
Now because of optimization thread 1 now do not check the value of a at line 1.

And goes into infinite loop. A serious problem caused due to optimization. 

So for avoiding this problem use volatile keyword to the variables which we do not want compiler to optimize in any case.



I hope it helps. If you find this useful share it. If you have any problem feel free to comment. How do you rate this post either interesting or cool or funny. See below. Thanks for visiting.


No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...