“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register.
Examples of volatile variables are:
• Hardware registers in peripherals (for example, status registers)
• Non-automatic variables referenced within an interrupt service routine
• Variables shared by multiple tasks in a multi-threaded application
The volatile variable can be changed outside the runninf of code.for instance if one static variblae x is being used within a function but its vale is not being changed, in this case compiler will optimize the code. But if value is being changed by say CPU or some other thread, compiler can not setect that. SO to avois such condition, the volatile is introduced , which allows compiler not to optimize the code
A volatile specifier is a hint to compiler that an object may chance its value in ways not specified by the language to that aggressive optimization must be avoided.
Submitted By: Talmeez ul Hassan — September 17, 2008
The volatile specifier disables various optimizations that a compiler might automatically apply and thus introduce bugs. Iwill give two examples:
1) Some hardware interfacing applications periodically read values from a hardware port and copy it to a local variable for further processing. A good example is a timer function that reads the current count from an external clock. If the variable is const, the compiler might skip subsequent reads to make the code more efficient and store the result in a CPU register. To force it to read from the port every time the code accesses it, declare the variable const volatile.
2) In multithreaded applications, thread A might change a shared variable behind thread B’s back. Because the compiler doesn’t see any code that changes the value in thread B, it could assume that B’s value has remained unchanged and store it in a CPU register instead of reading it from the main memory. Alas, if thread A changes the variable’s value between two invocations of thread B, the latter will have have an incorrect value. To force the compiler not to store a variable in a register, declare it volatile.
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register.
Examples of volatile variables are:
• Hardware registers in peripherals (for example, status registers)
• Non-automatic variables referenced within an interrupt service routine
• Variables shared by multiple tasks in a multi-threaded application
The volatile variable can be changed outside the runninf of code.for instance if one static variblae x is being used within a function but its vale is not being changed, in this case compiler will optimize the code. But if value is being changed by say CPU or some other thread, compiler can not setect that. SO to avois such condition, the volatile is introduced , which allows compiler not to optimize the code
quote from book “The C++ programming language”:
A volatile specifier is a hint to compiler that an object may chance its value in ways not specified by the language to that aggressive optimization must be avoided.
The volatile specifier disables various optimizations that a compiler might automatically apply and thus introduce bugs. Iwill give two examples:
1) Some hardware interfacing applications periodically read values from a hardware port and copy it to a local variable for further processing. A good example is a timer function that reads the current count from an external clock. If the variable is const, the compiler might skip subsequent reads to make the code more efficient and store the result in a CPU register. To force it to read from the port every time the code accesses it, declare the variable const volatile.
2) In multithreaded applications, thread A might change a shared variable behind thread B’s back. Because the compiler doesn’t see any code that changes the value in thread B, it could assume that B’s value has remained unchanged and store it in a CPU register instead of reading it from the main memory. Alas, if thread A changes the variable’s value between two invocations of thread B, the latter will have have an incorrect value. To force the compiler not to store a variable in a register, declare it volatile.
Leave an Answer/Comment