Subj : Re: double-checked locking in C To : comp.programming.threads From : David Schwartz Date : Wed Jul 06 2005 11:14 am "Laurent Deniau" wrote in message news:dagj50$2uj$1@sunnews.cern.ch... > Right. But this is not what I say. I was talking about value of > initialized before and after lock. It is clear that if extern_init_code() > has some side effects, it must lock/unlock the ressource it deals with > otherwise you break the semantic. For example if initialized is a pointer > to a singleton instance and extern_init_code() is a factory calling > malloc, you do not need a mutex, but malloc will certainly use one > internally. Therefore the correct code is (assuming a different > translation unit): > > int i=0; > pthread_mutex_t init_mutex; > > int extern_init_code(void) > { > pthread_mutex_lock(&init_mutex); > i=1; > pthread_mutex_unlock(&init_mutex); > > return 1; > } You still have the same problem. This didn't fix the problem I was talking about. The exact same sequence still has the exact same problem. Perhaps you don't understand the semantics of POSIX mutexes. They do *nothing* unless you have one thread that holds a lock when it changes a variable and then another thread that holds that same lock when it reads it. DS .