Volatile Variables
Volatile variables get special attention from the compiler
- data that gets updated may be allocated in a register as part of compiler optimization
- a write to shared data in one thread may therefore not be visible by another thread until the register is flushed to memory
- this is mostly incorrect behaviour that can lead to a deadlock or incorrect results
- variables such that an update has to propagate immediately to all threads have to be declared volatile
- compiler performs no optimizations on volatile variables, nor on the code around them, such that every write to volatile variable is flushed to memory
- there is a performance penalty associated with volatile variables
- Syntax:
- Fortran VOLATILE var-list
- C volatile “type” var-list