Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Wed Feb 09 2005 07:03 pm "David Schwartz" writes: >> If it caused a deadlock, the program would have to enter initialization >> of a particular local static variable before the previous initialization >> of that variable finished. But with the traditional non-thread-aware >> implementation of local static initializers this has undefined behavior. >> So it doesn't break anything which wasn't already broken. > > This analysis is not correct, it relies upon two different concepts of > when initialization is finished. > > Remember, you need to wrap the entire initialization function in a lock > of some sort, not just the process of determinine whether the initialization > has taken place or not. Sure, this is what I meant. > It is perfectly legal for code inside the constructor to dispatch > another thread and for that other thread to call functions which > invoke static initializers. And this is not a problem: each block of initializers have a different pthread_once_t. There would be a deadlock only if the same initializer is reentered, but C++ already makes this undefined. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .