Subj : Re: reliability of setting thread priority To : comp.programming.threads From : gottlobfrege Date : Sat Feb 26 2005 10:26 pm So to summarize this thread, how's this: if (AtomicRead(&initted)) return; if (!AtomicExchange(&entry, true)) // no one was in before us? { do_the_important_init_stuff(); AtomicExchange(&initted, true); if (AtomicRead(&event)) // anyone waiting? { SetEvent(event); } } else { // wait for init. // while waiting, create an event to wait on if (!AtomicRead(&event)) // anyone else waiting already? { tmpEvent = CreateEvent(NULL, true, false, NULL); // manual reset (ie will never be reset) if (AtomicCompareExchange(&event, 0, tmpEvent) == 0) { // wasn't set, is now } else { // someone snuck in // get rid of our attempt CloseHandle(tmpEvent); } } if (!AtomicRead(&initted)) { Wait(event); } } Looks to me, like 1. hopefully, and most importantly, it works :-) 2. if there is no contention, there is very very little cost 3. as this is all about initialization, I'm not too worried about Alexander's concerns about throwing in a destructor. (But I suspect some error handling could be added - as an exercise for the reader) 4. I could merge the bools 'initted' and 'entry' into a single 3 state int. Thoughts? Anyone still interested? :-) Thanks, Tony. .