question on semantics

Chris Friesen cfriesen@nortelnetworks.com
Wed May 4 18:01:00 GMT 2005


I'm not sure who I should address this to...I hope this is correct.

If I share memory between two processes, and protect access to the 
memory using standard locking (fcntl(), for instance), do I need to 
specify that the memory is volatile?  Or is the fact that I'm using 
fcntl() enough to force the compiler to not optimise away memory accesses?

As an example, consider the following code, with <lock>/<unlock> 
replaced with the appropriate fcntl() code:


int *b;

int test()
{
     b=<address of shared memory>;

     while(1) {
         <lock>
         if (*b) {
             break;
         }
         <unlock>
     }
     <unlock>

     return *b;
}

Without the locks, the compiler is free to only load *b once (and in 
fact gcc does so).  Is the addition of the locks sufficient to force *b 
to be re-read each time, or do I need to declare it as

volatile int *b;

Thanks,

Chris



More information about the Gcc mailing list