Subj : Re: Boost.Threads on its way to C++0x To : comp.programming.threads From : gottlobfrege Date : Wed Apr 27 2005 05:07 pm Torsten Robitzki wrote: > 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 I don't see that. The first thread (sees started as 0 and) sets *started* to true, and goes into the init code. The next thread sees started as true, goes and waits for *initted* to become true. What am I missing? It wasn't meant to be a good example, but I thought at least that much worked. .