2 ms·
> In a language like C, you can declare a variable to be volatile. When the program is compiled, this causes the compiler to emit memory barrier instructions -
by cesarb 8y ago
> In a language like C, you can declare a variable to be volatile. When the program is compiled, this causes the compiler to emit memory barrier instructions - these barriers force the CPU to do the work to ensure some semblance of sequential memory access is restored across the two sides of the barrier.
That is the case in Java, but not in C (which is a source of confusion to those who learned Java first). In C, volatile only guarantees that the compiler will not use a cached value when reading from the variable, and will not omit an apparently redundant write when writing to the variable. It's mostly useless except when dealing with memory-mapped hardware devices (where reads and writes have side effects), with Unix signals on single-threaded programs (for instance, having SIGTERM set a variable read by the main loop - without volatile, the compiler might read only once), or with setjmp/longjmp.
- toadworrier 8y agoYes, it's useful for the problems that were around when it was invented and useless for the new problems that have arisen since. I want my old problems back.