Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Fri Feb 04 2005 09:00 am Giancarlo Niccolai writes: > To put that thing in a compiler, you have to mathematically demonstrate > that: > > 1) ALL ever possible ST code can be made threadsafe by just adding a mutex > at its initialization code. > 2) ALL ever possible MT code won't be broken by your "help". No, 1 is not necessary for this change to be useful. And I believe 2 is true. > Suppose that read_unicode_database_from_file() requires ANOTHER static > initializer, or is replaced in the MT code with something different. In > example, suppose that read_unicode_database_from_file() requires a locked > variable to be prepared by the thread that calls > lookup_unicode_char_by_name(). Then, chances are that > read_unicode_database_from_file() will deadlock. If there was a cyclic dependency between local static objects, then the library would have been already broken in a single-threaded environment. I claim that it doesn't break code which is correct in ST. > You can EASILY avoid this situation by calling lookup_unicode_char_by_name() > before any thread is started; you won't need any synchronization > thereafter. This defeats the advantage of lazy initialization. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .