1Using volatile Keyword is yet another way (like synchronized,
2atomic wrapper) of making class thread safe. Thread safe
3means that a method or class instance can be used by
4multiple threads at the same time without any problem.
5
6
1C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.
1//volatile keyword usage in C
2#include<stdio.h>
3
4int main(){
5 //different methods of declaring and initializing volatile variables
6
7 //method 1 - volatile int
8 int volatile number1 = 10;
9
10 //method 2 - volatile int
11 volatile int number2;
12 number2 = 20;
13
14 //method 3 - volatile pointer
15 int volatile *p1;
16 p1 = &number1;
17
18 //method 4 - volatile double pointer
19 volatile int **p2;
20 p2 = &p1;
21
22 printf("%d %d %d %d",number1,number2,*p1,**p2);
23 return 0;
24}