This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Optimization of conditional access to globals: thread-unsafe?
- From: Ian Lance Taylor <iant at google dot com>
- To: "Bart Van Assche" <bart dot vanassche at gmail dot com>
- Cc: "Tomash Brechko" <tomash dot brechko at gmail dot com>, gcc at gcc dot gnu dot org, "Andrew Pinski" <pinskia at gmail dot com>
- Date: 26 Oct 2007 08:20:05 -0700
- Subject: Re: Optimization of conditional access to globals: thread-unsafe?
- References: <e2e108260710260541n61462585u99de9bc0617720f4@mail.gmail.com> <e2e108260710260620k2a2e21b3t1d6c052f14d36094@mail.gmail.com>
"Bart Van Assche" <bart.vanassche@gmail.com> writes:
> * As known the compiler may reorder function calls and assignments
> to non-volatile variables if the compiler can prove that the called
> function won't modify that variable. This becomes problematic if the
> variable is modified by more than one thread and the called function
> is a synchronization function, e.g. pthread_mutex_lock(). This kind of
> reordering is highly undesirable. This is why any variable that is
> shared over threads has to be declared volatile, even when using
> explicit locking calls.
What happens in practice is that pthread_mutex_lock and friends are
magic functions. In gcc, this magic implemented using inline
assembler constructs.
Ian