Subj : Re: double-checked locking in C To : comp.programming.threads From : Joe Seigh Date : Tue Jul 05 2005 07:10 pm amorox@gmail.com wrote: > Hi, > > I would like to use the DCL idiom (double-checked locking) to guarantee > the once and only once execution of an initialization code: > > bool initialized = false; > pthread_mutex_t mutex; > > void function(void) > { > if (!initialized) { > pthread_mutex_lock(&mutex); > if (!initialized) { > initialized = true; > init_code(); > } > pthread_mutex_unlock(&mutex); > } > } > > I know that in Java there were problems with this idiom due to the > memory operation reorderings and these have been apparently solved in > Java 1.4. > > Is there any problem using this idiom in a multithread C program? If > affirmative would you explained why? > > Thanks, > Ovi > > P.S.: I know the problem may be solved by using 'pthread_once' > protection. > http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .