(P876 from textbook C++ How To Program:) The volatile type qualifier is applied to a definition of a variable that may be altered from outside the program (i.e., the variable is not completely under the control of the program). Thus, the compiler cannot perform optimizations (such as speeding program execution or reducing memory consumption, for example) that depend on "knowing a variable's behavior is influenced only by program activities the compiler can observe."
(notes:) 1) volatile indicate the object is modified by something not directly under the compiler's control (i.e., the hardware itself) 2) one use of volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers. 3) Another example might be the global variables that keeps track of the total number of timer interrrupts.
One use of the volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.
It can be useful to main multi threaded program for many reasons: 1) The volatile variable has high priority on Read/Write operations. 2) Its value is immediately modified after the assignment operation. 3) we can use it as Critical section syncronization object. 4) The thread proc function can be handled properly with volatile variable when compared to global variables.
Making a variable volatile get the current value of the variable.when ever the compiler would look for the value of the variable it would get the current value associated with the variable Memory optimizations dont work with it.