NVIDIA Agilent Technologies Interview Question
Software Engineer / Developers"A Java(TM) programming language keyword used in variable declarations that specifies that the variable is modified asynchronously by concurrently running threads."
"Subject to change. The volatile keyword informs the compiler that a variable could change value at any time (because it is mapped to a hardware register, or because it is shared with other, concurrent processes) and so should always be loaded before use."
The value stored in the variable marked as volitile can change at any time.(Example: it is connected to some external circuit which causes new values to be stored at that location at random times). Therefor the compiler should NOT perform any read optimizations that would cache the value. Each time the value is to be read, the value should be read directly from memory/register.
I wasn't sure what volatile really was until recently.
From my understanding, it ensures that the program works with the most recent copy of a value. Specifically, if an interrupt service routine changes the value of a variable and function uses a register instead of the variable, the value will be lost to the function. Volatile ensures that the compiler doesn't perform this optimization.
Wouldn't you use a critical section (semaphores, mutex, etc)for these types of variables if multiple threads are accessing it? Or volatile meaning something outside the process can modify the variable? Any examples?
Threaded programming solves only part of the problem. The biggest use of volatile is in low-level programming whereby you're trying to send a signal to an i/o device or the I/O device is asynchronously modifying the value of a memory location. in these situations you dont want to " act smart" and try to optimize accesses by storing copies in registers that could be stale.
Also, say you're trying to send a digital signal to a HW peripheral
int signal = 0;
//insert code to pause for some time
signal = 1;
compiler may optimize this simply to signal = 1 so you lose your intended functionality. Thus need volatile keyword
Uses of the ‘volatile’ Keyword
The volatile specifier disables various optimizations that a compiler might automatically apply and thus introduce bugs. Ill 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 Bs back. Because the compiler doesnt 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 variables 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.
Danny Kalev
Am not quite sure whether you are right.
- Joseph Patrao February 06, 2006i think volatile instructs the compiler not to optimse the storage of a variable declared as volatile (i.e. not to sorta cache it) since it is subject to change by another process.