Subj : Re: double-checked locking in C To : comp.programming.threads From : Thomas Kemmer Date : Wed Jul 06 2005 03:57 pm Laurent Deniau writes: > David Schwartz wrote: [...] > >>void function(void) > >>{ > >> if (!initialized) { > >> pthread_mutex_lock(&mutex); > >> if (!initialized) > >> initialized = extern_init_code(); > >> pthread_mutex_unlock(&mutex); > >> } > >>} [..] > > And what guarantees that a CPU that never acquired the mutex sees > > the initialization itself? > > I do not understand your point. If it does not acquire the mutex, it > means that it has seen a valid value for initialized. So I do not see > a problem. Could you give a sequence underlining the problem ? > > ld. The point here is--as far as I understand it--that you usually call extern_init_code() for its side effects; i.e. setting some global/singleton variables, etc. I guess this is what David means with "the initialization itself". A CPU that never acquired the mutex will, of course, see "initialized" set to true, as you say. However, there is no guarantee that this CPU will also see the values set by extern_init_code(), which is what this is really all about, I think... Regards, - Thomas .