Subj : Re: Insane "Metered Section" example on MSDN To : comp.programming.threads From : Joe Seigh Date : Tue Apr 19 2005 08:09 am On 19 Apr 2005 01:48:36 -0700, Agresao wrote: > 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... > > There's a lot bad examples on MSDN. Almost anything to do with using Events as condition variables is probably wrong. For a semaphore that doesn't do a kernel call unless it needs to block see http://groups.google.com/groups?selm=412F5D8D.416AE16A@xemaps.com -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .