Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Sat Feb 05 2005 12:30 am Giancarlo Niccolai writes: >>> 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. > > The cyclic dependency is introduced in MT code; ST code cannot have cyclic > dependency. If you introduced a cyclic dependency, you broke the code, and it has nothing to do with making initialization of static local objects thread-aware. Please show how it's broken when thread local static objects are initialized in a thread-aware way, and at the same time it works when they are initialized using an traditional guard variable. > In example, are you really sure that it's a "smart" programming technique to > open and setup a database environment as a by-product of a very commonly > used routine? How will you manage errors? With try-catches? are you really > sure you want to try-catch all your access to a unicode-translation > functions to prevent a lazy initialization failure to abort your program? This has nothing to do with MT. If you don't like static local variables with non-trivial initialization, don't use them, but why do you advocate a broken semantics of these variables when they are used in multiple threads? > I repeat myself, but you are trying to solve an unexisting problem; > correctly built MT programs just works another way, and ST programs going > MT just must be redesigned in some area; Not all programs must be redesigned. There is a chance that making initialization of local static objects thread-aware is the only thing needed to make them working. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .