Faulty implmentation of ThreadPool.RegisterWaitForSingleObject

  • Thread starter Thread starter Jeff Skaistis
  • Start date Start date
J

Jeff Skaistis

I think I've found a problem in the framework implementation of the
ThreadPool RegisterWaitForSingleObject method. Given the following C# code:

static void Main(string[] args)
{

ManualResetEvent evt1 = new ManualResetEvent(false);
ManualResetEvent evt2 = new ManualResetEvent(false);

ThreadPool.RegisterWaitForSingleObject(evt1, new
WaitOrTimerCallback(EventProc), "Event 1", 30000, false);
ThreadPool.RegisterWaitForSingleObject(evt2, new
WaitOrTimerCallback(EventProc), "Event 2", 30000, false);

evt1.Set();
evt2.Set();

Thread.Sleep(20);

evt2.Reset();
evt1.Reset();

}


private static void EventProc(object state, bool timedOut)
{
Console.WriteLine(state);
}


I'd expect to see messages for both Event 1 and Event 2 during the time they
are both set. However, only messages from Event 1 show up. According to
the docs, the RegisterWaitForSingleObject method is implmented using the
Win32 WaitForMultipleObjects call to monitor all of the registered objects,
with the fWaitAll parameter set to false I'd assume. However in that case,
the WaitForMultipleObjects call only returns at most one indicator of a
signaled object even if more than one of the objects are signaled. That
means that when both evt1 and evt2 are signaled in the above code, the
program will only get notifications of the lowest indexed one in the
WaitForMultipleObjects handle array.

Obviously using manual event handles makes the problem stand out more, but
this could definitely cause problems if someone expects notifications no
matter how many other objects are signaled at the same time.

I beleive that the implementation should be corrected, or at least have the
framework documentation updated to point out this issue.


Jeff Skaistis
 
Back
Top