[fpc-devel] volatile variables

Hans-Peter Diettrich DrDiettrich1 at aol.com
Wed Jun 29 13:28:20 CEST 2011


Vinzent Höfler schrieb:

>> Question is, what makes one variable use read/write-through, while 
>> other variables can be read from the cache, with lazy-write?
> 
> Synchronisation. Memory barriers. That's what they are for.

And this doesn't happen out of thin air. How else?

>> Is this a compiler requirement, which has to enforce 
>> read/write-through for all volatile variables?
> 
> No.  "volatile" (at least in C) does not mean that.

Can you provide a positive answer instead?

I meant "volatile" as read/write-through, not related to C. Read as 
"synchronized", if you ar more comfortable with that term ;-)

>> But if so, which variables (class fields...) can ever be treated as 
>> non-volatile, when they can be used from threads other than the main 
>> thread?
> 
> Without explicit synchronisation? Actually, none.

Do you understand the implication of your answer?

When it's up to every coder, to insert explicit synchronization whenever 
required, how to determine the places where explicit code is required?

Aren't we then in the unpleasant situation, that *every* expression can 
be evaluated using unsynchronized values, unless the compiler uses 
synchronized read instructions to obtain *every* value [except from stack]?

DoDi




More information about the fpc-devel mailing list