Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Thu Feb 03 2005 10:40 pm Giancarlo Niccolai writes: > there is NO reason, NO REASON AT ALL, to forcefully protect all the > data that may possibly be ever shared (especially if the nature of > this protection is doubtful). It's not about protection of all data, but of local static objects (initialized on first use). > Doing it more is not just an overkill: is an error that can lead to > cross-locking or other potential problems. Please show an example code which works when local static objects are protected using the traditional way which assumes no threads (a hidden boolean variable initialized to false, which is checked when the control flow enters the function, and is changed to true while performing the initialization if this is the first time) but stops working when this is done in a way which lets the current thread wait when another thread is currently initializing the variable. > One of the most important things that you have to understand is that "shared > data" may be more than a single class, and that a single class may present > several sets of shared data each one to be protected in a different context > and in a different way. Do you really think that a compiler can ever get > smart enough to cope with this complexity without breaking things? It doesn't have to be smart. Just insert an equivalent to pthread_once to guard initialization of local static objects which require dynamic initialization. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .