Subj : Re: Boost.Threads on its way to C++0x To : comp.programming.threads From : Torsten Robitzki Date : Wed Apr 27 2005 07:40 pm gottlobfrege@gmail.com wrote: > > void ensure_initted() > { > if (!AtomicExchange(&started, true)) > { > InitCriticalSection(&cs); > AtomicExchange(&initted, true); > } > else > { > // There's better ways to do this than spinning, > // but this is make the example shorter. > // In fact, this example has a starvation problem, > // if this thread has a higher priority > // than the init thread. > // (ie may never get a chance to be initted) > > while (!AtomicRead(&initted)) > { > Sleep(1); > } > } > } When a first thread executes the first call to AtomicExchange() every other thread will leave the function immediately after the call to this function without ensuring that the first thread finished the call to InitCriticalSection(). So the call to EnterCriticalSection() can operate on an unitialized cs. regards Torsten .