Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Wed Feb 16 2005 10:21 am gniccolai@yahoo.com (Giancarlo Niccolai) writes: > Someone proposed to have a mutex to lock a static initializer > constructor call (not pthread_once like Marcin only lately suggested, > which brings other problems that I don't want to discuss now); It is equivalent (modulo cancellation, i.e. the mutex should be released when the initializer is cancelled). > So, the thing here is not what happens in that COFU, but that there > is someone saying (and from Mariani sayings, CURRENTLY putting in a > compiler) THIS scheme: > > the_safe_initializer() { > acquire_mutex() > if (! cls.initialized) //a classwide static or something the like > cls.construct() // a classwide constructor or something the like > release_mutex() > } > > Now, we all know here that THIS may deadlock, No, it can only deadlock with a program which already has undefined behavior. > as we don't know what cls.construct() is doing, and as someone may > i.e. legitimaly do: > > acquire_recursive_mutex_used_in_cls_constructor() > the_safe_initializer() > cls.something = change() > release_recursive_mutex_used_in_cls_constructor() It may not reenter the_safe_initializer. Reentering a static local initializer before its previous run has finished is forbidden by the C++ standard. You will not find a code which is broken by this change because it cannot exist. > Also, someone sustained that a scheme like the one in > the_safe_initializer() is able to secure any single thread code > against improper use of concurrency; I don't believe anybody has claimed this. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .