[fpc-devel] volatile variables

Andrew Brunner andrew.t.brunner at gmail.com
Tue Jun 28 15:23:46 CEST 2011


On Tue, Jun 28, 2011 at 8:16 AM, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
> The C (or Pascal) compiler has no idea whether or not the global variable
> will be accessed by the pthread_mutex_lock()/unlock() function. As a result,
> it will never cache it in a register across function calls, and the call to
> the mutex function by itself guarantees that the variable's value will be
> written back.

Agreed.  And you can make such access atomic.  Getting developers to
chose the right tool for the job is the key here.  But understanding
the core issues (pun intented ;-) is the key.


> So you don't need volatile. Even if it's a local variable you don't need it,
> because as soon as you take the address of a local variable and it can
> escape the current function (and hence the current thread), the compiler
> again has to assume that any called function may modify it via an indirect
> access.
>

There is no problem no need for volatile variables.  Compare and Swap
or Interlocked mechanisms will solve any problems.



More information about the fpc-devel mailing list