Jonathan Wakely wrote:
On 5 March 2013 09:27, David Paterson wrote:
In addition to Jonathan's answer on the use of "volatile", it's worth adding
that it's not only used for memory mapped hardware. There are many other
uses, such as inter-thread communication, or indeed the example you
show below.
Only in broken code.
volatile is not for multithreading, you need proper synchronization
for interthread communication.
Maybe he meant something like using volatile to prevent code moves with
asynchronous communication (be it I/O or communication with libraries
like MPI, or also call to some functions, which internally invoke some
kind of synchronization.)
However, if I understand it correctly, GCC doesn't do such code moves*
and C/C++ also does not allow them – even not without "volatile". That's
different to Fortran, which allows for some code moves across procedure
calls; hence, Fortran has – additionally to "volatile" – the
"asynchronous" attribute to prevent those.
Tobias
* Side note: See PR49733 which tracks a case, where for Fortran code the
lack of the asynchronous attribute is not used for optimizations.