how to cast away 'volatile'?

Ian Lance Taylor
Tue Mar 13 16:09:00 GMT 2007

"Kevin P. Fleming" <> writes:

> Ian Lance Taylor wrote:
> > That means that the only correct way to share a memory buffer between
> > threads is to use a mutex, and it means that the mutex code can not be
> > written in standard conformant C.  The mutex code must use asm
> > statements to insert memory barriers as required (on the x86 a lock
> > prefix serves as a memory barrier (except that some specific x86
> > processors have bugs, but I digress...)).
> This comment makes me curious, since I see (and work on) plenty of code
> that shares data structures between threads using only a mutex for
> protection and no memory barrier instructions of any kind. This code is
> using standard POSIX threading constructs and seems to work fine on
> multi-CPU and multi-core systems, both x86 and other flavors.

The mutex code itself, assuming it is written correctly, provides the
required memory barriers.

> Are you referring to a specific sort of memory sharing between threads
> here, or are you saying that you think that _all_ objects shared between
> threads need to be protected in this way?

All objects shared between threads need to protected by a mutex, or by
some other type of memory barrier.


More information about the Gcc-help mailing list