how to cast away 'volatile'?

Ian Lance Taylor iant@google.com
Wed Mar 14 01:12:00 GMT 2007


Michael Eager <eager@eagercon.com> writes:

> > There's nothing to stop the machine from
> > reordering memory writes, even if the compiler doesn't.
> 
> Yes, it's safe.  It doesn't matter which microsecond that the flag is set.
> (Or if it does, then your code should be using a mutex or some other
> form of synchronization.)
> 
> Say you have one thread which performs some process when it sees
> a shared flag set.  It checks this flag every second.  If another
> process sets this flag, the service thread will notice and take action.
> 
> If the signaling process and the receiving process both try to
> access the shared memory at the same time, the processor may
> reorder the operations as it chooses.  The worst case is that
> the server will check and see that the flag is not set, then
> the signaling process will set the flag.  The result is a short
> delay in the receiving process noticing that the flag is set.
> 
> Note that there is a race condition which is problematic.  If the
> receiving process clears the flag, at the same time that the
> signaling process sets it, then these may be reordered.  So you
> need to insure that only one of the processes ever writes to the
> flag.

I think that will be OK in practice provided the only memory shared
between the two processes is the signalling flag itself.

Ian



More information about the Gcc-help mailing list