Subj : Re: Boost.Threads on its way to C++0x To : comp.programming.threads From : gottlobfrege Date : Tue Apr 26 2005 08:00 am Peter Dimov wrote: > gottlobfrege@gmail.com wrote: > > - a static_local template: > > > > int func() > > { > > // this thread-safely inits foo only once > > static static_local foo(foo_param1, foo_param2,...); > > How do you solve the race condition WRT the hidden compiler-generated > boolean flag? There are lots of ways, but they all boil down to the same thing: ie you ignore the compiler's flag and use your own. And use call_once to set your flag. More likely, you use call_once to init a CriticalSection, then use the CriticalSection to init (in-place new?) your internal Foo object. Or use a 'StaticLocalCriticalSection' object (which does the call_once stuff internally, or does the call_once-like magic itself, and then write call_once using the StaticLocalCriticalSection...). Or use a Once object and a Once::Sentry object, which is created using a StaticLocalCriticalSection, or vice versa, etc. Even though all the ways are 'equivalent', some take longer than others, and, if you are careful, you can avoid creating any mutexes/semaphores/criticalsections in the non-contention case. It depends what you know about the context - ie is the call_once init_func going to throw, or just call InitCriticalSection() and you're comfortable assuming that works, etc... .