Re: how to cast away 'volatile'?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux