how to cast away 'volatile'?
Andrew Haley
aph@redhat.com
Tue Mar 13 17:32:00 GMT 2007
Michael Eager writes:
> Andrew Haley wrote:
> > Ian Lance Taylor writes:
> > >
> > > All objects shared between threads need to protected by a mutex, or by
> > > some other type of memory barrier.
> >
> > It's an interesting contention -- that in GNU/Linux userspace there
> > are essentially no legitimate uses for volatile. Hmmmm...
>
> I think that is an over-generalization.
>
> There are uses for volatile in user code, but they are limited.
> Volatile is needed in the code which implements a mutex.
I think that falls into Ian's category of "magic".
> Volatile is needed when accessing a machine register, or an OS
> variable (e.g., system time, errno) so that the latest value is
> fetched or set, rather than being optimized away.
>
> It can also be used to share data between threads, but it becomes
> problematic if accesses are not atomic. It's reasonable to use a
> shared volatile sig_atomic_t to communicate between threads, to say
> "I'm done" or "Start processing".
Is that really safe, though? There's nothing to stop the machine from
reordering memory writes, even if the compiler doesn't.
Andrew.
More information about the Gcc-help
mailing list