Subj : Re: Can C++ local static objects be made thread safe? To : comp.programming.threads From : Alexander Terekhov Date : Thu Feb 24 2005 10:51 pm Marcin 'Qrczak' Kowalczyk wrote: [...] > The problem is in the lack of a common C++/POSIX standard which would > clearly say that static local initializers are thread-safe. You still don't get it. Consider: ThreadLocalLazySingleton & ThreadLocalLazySingleton::get() { static(thread) ThreadLocalLazySingleton thing(/**/); return thing; } LazyImmutableSingleton const & LazyImmutableSingleton::get() { static(synchronized) const GlobalLazyImmutableSingleton thing(/**/); return thing; } ThreadsUnawareLazySingleton & ThreadsUnawareLazySingleton::get() { static ThreadsUnawareLazySingleton thing(/**/); return thing; } static ReadWriteLock s_lock; LazySingletonWithReadWriteAccess * LazySingletonWithReadWriteAccess::s_inst = 0; // set in ctor auto_unlock_ptr< LazySingletonWithReadWriteAccess > LazySingletonWithReadWriteAccess::getForWrite() { ReadWriteLock::WriteGuard guard(s_lock); static LazySingletonWithReadWriteAccess thing(/**/); return auto_unlock_ptr< LazySingletonWithReadWriteAccess >(&thing, guard); } auto_unlock_ptr< const LazySingletonWithReadWriteAccess > LazySingletonWithReadWriteAccess::getForRead() { ReadWriteLock::ReadGuard guard(s_lock); if (s_inst) return auto_unlock_ptr< const LazySingletonWithReadWriteAccess >(s_inst, guard); guard.dismiss(); return getForWrite().degradeToReadOnlyAccess(); } Hth. regards, alexander. .