Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Gianni Mariani Date : Sat Feb 12 2005 01:21 pm Giancarlo Niccolai wrote: > "David Schwartz" wrote in message news:... > >>"Giancarlo Niccolai" wrote in message >>news:95e4efda.0502101630.6e6c144d@posting.google.com... > > >> This code is already broken. The '&cofu()' method may modify >>'*the_item', so must be called while holding some sort of lock. The >>'thread_func_1' function calls '&cofu' without holding any lock, so its >>interaction with 'thread_func_2' is undefined under POSIX. >> > > > The COFU must already have been called BEFORE any thread is started. > I've dissucessed it MUCH before; I was giving it for granted that > *the_item has been created safely before those two threads are > started... Calling all fuctions with static locals before main() is called or before starting threads is not always practical. It's almost impossible when you use templates. > > The fact is that a COFU would work in that case. But a static > initializer instead of a COFU would not, that was the point of "change > it with any static initializer method". I still miss your point. The point is that a static > initializer would require lock while checking for the item to have > been already created, and that's where the deadlock with the reentrant > mutex happens. If it's a recursive mutex, no deadlock happens. .