Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Fri Feb 11 2005 10:25 am gniccolai@yahoo.com (Giancarlo Niccolai) writes: > // Change this with your favorite static initializer method. > Item &cofu() { > static Item *the_item = 0; > if ( the_item == 0 ) { > the_item = new Item(); // compiler lock here. No, the compiler only adds a pthread_once around a definition of a static local which requires dynamic initialization, i.e. where it's currently specified to be run on the first time the control flow passes it. There is no lock around assignment. Since the initialization (the first line of the function) uses a compile-time constant, there is even no synchronization at all. So the example is compiled in exactly the same way with both choices of the semantics of initialization of static locals. You are unable to produce an example which breaks with the synchronized semantics because it doesn't break anything. > I had this test since I first presented the other example that > deadlocked if using only one mutex for every static initializers, > but I hoped you could see this simple thing by yourself, AT LEAST > after that a similar case were given to you. > > You lost an occasion to seem smart, really. > > That was *really* my last. Adieu ROTFL. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .