Subj : Re: double-checked locking in C To : comp.programming.threads From : Joe Seigh Date : Wed Jul 06 2005 07:21 am Laurent Deniau wrote: > > > 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. > > 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. 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. > No. While most processors have dependency orderings, not all do. And they're usually not part of the processor's formal memory model so you have to be extra careful. And the dependency stuff doesn't provide release semantics which is usually where people mess up here, your example for instance. 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. .