Subj : Re: Avoiding synchronization (pthreads) To : comp.programming.threads From : Joe Seigh Date : Wed Mar 02 2005 10:26 am On Wed, 02 Mar 2005 15:39:44 +0100, Marcin 'Qrczak' Kowalczyk wrote: > If a global variable of type int is read without holding a mutex, at > the same time as other threads may write into it (holding the mutex), > can anything worse than getting an out-of-date value happen in practice? > > The variable in question counts the number of threads. It is used > to avoid locking in case there is only one thread. There may be > background threads terminating asynchronously which decrement the > variable. If the first paragraph is true, I should be safe, because > an out-of-date value can only be larger than the true value, and > in this case I will execute an equivalent but slower code (which > takes the mutex, so the next time the variable will be up to date). > You'll need an acquire barrier after reading that there is only one thread. int has to be atomic of course. -- Joe Seigh .