05-03-05 11:00 PM
When sharing memory between processes, is the use of locking primitives
(fcntl, flock, sysV semas, NPTL process-shared operations, etc.)
sufficient to ensure that gcc will not optimize away the load from the
shared memory area?
Or do I need to also make it volatile?
As an example, consider the following code, where the locking operations
could be any one of the ones listed above:
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
[ Post a follow-up to this message ]
|