Subj : Re: double-checked locking in C To : comp.programming.threads From : David Schwartz Date : Wed Jul 06 2005 05:55 am "Laurent Deniau" wrote in message news:dag5mv$2lf$1@sunnews.cern.ch... > There is a simple, portable, processor independent, compiler independent > idiom which consists of delegating the work to an extern function, a > function with an implementation in a different translation unit. Err, what?! If it's not guaranteed to work by the relevent standard, it's not portable. > void function(void) > { > if (!initialized) { > pthread_mutex_lock(&mutex); > if (!initialized) > initialized = extern_init_code(); > pthread_mutex_unlock(&mutex); > } > } > The compiler is not allowed to assign initialized *before* > extern_init_code() returns. Not allowed by what? > The processor will not be able to do the same since this is a true jump > with a return value. The assignment could be delayed but not after the > unlock, so it is not a problem. Why can't the assignment be delayed until after the unlock? And what guarantees that a CPU that never acquired the mutex sees the initialization itself? DS .