Subj : Re: Insane "Metered Section" example on MSDN To : comp.programming.threads From : Neill Clift [MSFT] Date : Fri Apr 22 2005 08:43 pm "Agresao" wrote in message news:1113900515.926413.282240@o13g2000cwo.googlegroups.com... >I was trying to implement a semaphore that won't always do a kernel > transition to a Win32 object and came up with some code that seemed to > work. Then I found this article on MSDN about this thing called a > "metered section": > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_metrsect.asp > > The code sample looked very similar to my own efforts which sort of > made me think that my solution was correct. However by thinking a > little deeper one realizes that their design has a terrible race > condition, in which threads are blocked waiting for an auto-reset event > to be signaled when there's nobody to signal it. Testing showed that > this actually happens. > > I'm sure some of you have seen this article before and talked about it, > but what puzzles me is that it has been in MSDN since 1998 and nobody > has bothered to revise it... > I just looked through this and I agree. Any algorithm that calls SetEvent in a loop expecting to wake n threads is broken. I'll notify the appropriate people. Thanks. Neill. .