Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : gniccolai Date : Sat Feb 12 2005 10:51 am "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... 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". 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. (this also replies to Mariani next letter) Bests, Gian. .