Subj : Re: Avoiding synchronization (pthreads) To : comp.programming.threads From : Joe Seigh Date : Wed Mar 02 2005 03:52 pm On Wed, 02 Mar 2005 21:09:19 +0100, Marcin 'Qrczak' Kowalczyk wrote: > "Joe Seigh" writes: > >> >> You'll need an acquire barrier after reading that there is only >> one thread. > > What can happen without it? > > The variable is decremented asynchronously, but it is ever incremented > only by the thread which does the conditional locking. So if this > thread sees an out-of-date value, it can only see a too high value, > and it will work correctly in this case (in particular it always does > both locking and unlocking or neither). > > What else than seeing an out-of-date value can happen? > >> int has to be atomic of course. > > Yes. Are there architectures where one thread decrementing an int can > cause another thread which doesn't use a memory barrier see the value > which has nothing to do with the actual range of values of this int? > You need the barrier to prevent reads of the shared data occuring before the fetch of the count. You only need to do it once after seeing the count go to one thread. Kind of like DCL but with multiple initializers. -- Joe Seigh .