[fpc-devel] volatile variables

Henry Vermaak henry.vermaak at gmail.com
Tue Jun 28 13:20:24 CEST 2011


On 28/06/11 12:14, Michael Schnell wrote:
> A similar discussion is going on in Lazarus-develop, but this obviously
> is a compiler question.
>
>
> In C, there is the volatile keyword that ensures that after the code
> sequence enters the next C instruction after that which modified this
> variable, another thread sees the correct state of the variable.

This is completely incorrect.  Read this:

http://en.wikipedia.org/wiki/Volatile_variable

Note this:

"Operations on volatile variables are not atomic, nor do they establish 
a proper happens-before relationship for threading. This is according to 
the relevant standards (C, C++, POSIX, WIN32), and this is the matter of 
fact for the vast majority of current implementations. The volatile 
keyword is basically worthless as a portable threading construct."

Henry



More information about the fpc-devel mailing list