Subj : Re: double-checked locking in C To : comp.programming.threads From : Laurent Deniau Date : Thu Jul 07 2005 12:12 am David Schwartz wrote: > "Laurent Deniau" wrote in message > news:dagnq8$dgi$1@sunnews.cern.ch... > > >>How I can I prove that there is no bogus? If the DCL code shown is so easy >>to break, show me a sequence of actions (points). I can detail all steps >>for different cases, but it will be much longer than showing a single >>bogus case, if any. > > > I already showed you the sequence. A later thread sees 'initialized' as > 'true' but does not see the values that *were* initialized. The problem you > are trying to solve is fundamental. It goes like this: > > 1) The whole point of DCL is that subsequent threads don't have to > acquire any locks and still must see the effects of the initialization. > > 2) Nothing in the DCL patterns presented actually ensures that > subsequent threads see the effects of the initialization. > > Therefore subsequent threads might not see the effects of the > initialization. > > The fundamental error is thinking that mutexes do something even for > threads that do not acquire them. A mutex is only guaranteed to work if it > is accessed by a thread that holds the same mutex that was held by the > thread that changed the value. I agree. But the DCL+singleton pattern rely on a safe default value in case of missing synchronization. Either the thread sees the correct value of the flag either it sees the wrong default value and then locks. The second check reveals the correct value of the flag. The DCL pattern cannot be applied to a flag which does not change from safe_value to init_value only once during the program execution. Now, there is the problem of reflecting the change of the side effects to other threads. My suggestion was to put the flag inside the side object itself. Therefore if the init_value is seen, the object is also seen. Of course you may think that the object takes place on cache line boundary and is only partially updated. That is in the worst case, only the flag part and not the rest. But I am not sure if this can happen. ld. .