Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Wed Jan 19 2005 09:57 pm "David Schwartz" writes: > Okay, but what should be locked? Are you suggesting that every single > construction of a static object should be treated as if it were bracketed by > a lock/unlock pair of a single global mutex? It's enough to have one lock for initializing all static objects in a function. They are always initialized together and in the same order, so there is no lost opportunity of parallelization. Of course objects which require no dynamic initialization may be initialized statically and thus the lock is not needed if all static variables are like this. The lock would apply only to initialization, not to using the variable. In particular this is a thread-safe lazily initialized singleton: T *singleton() { static T *ptr = new T(); return ptr; } Implementation may use the mechanism analogous to pthread_once. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .