question on semantics

David Daney ddaney@avtrex.com
Wed May 4 20:29:00 GMT 2005


Chris Friesen wrote:
> Mike Stump wrote:

>> It is safer to.  People might compile your whole app at once for  
>> better optimization, and the compiler might be just smart enough to  
>> optimize it away.
> 
> 
> One problem with using volatile is that it can destroy performance.  I 
> only need to really read it in from memory the first time after I take a 
> lock, and flush it out to memory at the time I give up the lock.  If I 
> specify it as volatile, I force the compiler to read/write memory on 
> *every* access, even when it's not necessary.

If you are concerned about this, why not explicitly code it by copying 
the value in to a non-volatile temporary variable?  Or make the variable 
non-volatile and protect access with proper synchronization primitives.

I expect the compiler to do the best optimization possible within the 
bounds of the language specification.  As earlier posters have 
expressed, I don't think it is wise to count on the compiler doing a 
poor job of optimization in order to obtain desired program behavior.

David Daney



More information about the Gcc mailing list