Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Thu Feb 17 2005 11:34 am gniccolai@yahoo.com (Giancarlo Niccolai) writes: > False. The static initializer cannot be entered twice, but this is > irrilevant: the example deadlocks exactly if the app mutex + static > initializer mutex is entered the wrong way, even just once. In order for a mutex to cause a deadlock, a thread must be waiting on it (otherwise operations on the mutex are no-ops). If a program waits on an automatically inserted mutex, this means that the initialization of a static local is in progress (because the mutex is locked only during such initialization) and that a thread tries to execute this definition again. This is prohibited by current C++, you may not enter initialization of a static local before its brevious initialization has completed, the program is already broken. (The point of the change is to lift the restriction in case the other entry is done by a different thread than before. In this case it will wait for the thread performing initialization to finish.) (This is not a new idea. In Haskell all variables and most fields of data structures are lazy, evaluated the first time they are needed. When they added threads to the language, they gave the obvious semantics to evaluating a variable concurrently: the second thread waits for the first to finish.) There is no example. You have been unable to show it, because it can't exist. I just proved that it can't exist. You don't have the courage to admit an error, so you are just refusing to show it and closing the discussion. > And please, remember I am still talking about mutex guarded > initialization; probably, a pthread_once thing would work in this > context (depending on how it is implemented at low-level). It's the same in both cases. pthread_once is equivalent to a mutex with a boolean flag which governs whether the computation should be run this time, and is set after the computation completes. (The mutex is released if the computation is cancelled, without setting the flag, but cancellation is not the point of the discussion.) -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .