ManualResetEventSlim: Calling .Set() followed immediately by .Reset() doesn't release any waiting threads
(Note: This also happens with ManualResetEvent
, not just with ManualResetEventSlim
.)
I tried the code below in both release and debug mode. I'm running it as a 32-bit build using .Net 4 on Windows 7 64-bit running on a quad core processor. I compiled it from Visual Studio 2012 (so .Net 4.5 is installed).
The output when I run it on my system is:
Waiting for 20 threads to start
Thread 1 started.
Thread 2 started.
Thread 3 started.
Thread 4 started.
Thread 0 started.
Thread 7 started.
Thread 6 started.
Thread 5 started.
Thread 8 started.
Thread 9 started.
Thread 10 started.
Thread 11 started.
Thread 12 started.
Thread 13 started.
Thread 14 started.
Thread 15 started.
Thread 16 started.
Thread 17 started.
Thread 18 started.
Thread 19 started.
Threads all started. Setting signal now.
0/20 threads received the signal.
So setting and then immediately resetting the event did not release a single thread. If you uncomment the Thread.Sleep(), then they are all released.
This seems somewhat unexpected.
Does anyone have an explanation?
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Demo
{
public static class Program
{
private static void Main(string[] args)
{
_startCounter = new CountdownEvent(NUM_THREADS); // Used to count #started threads.
for (int i = 0; i < NUM_THREADS; ++i)
{
int id = i;
Task.Factory.StartNew(() => test(id));
}
Console.WriteLine("Waiting for " + NUM_THREADS + " threads to start");
_startCounter.Wait(); // Wait for all the threads to have called _startCounter.Signal()
Thread.Sleep(100); // Just a little extra delay. Not really needed.
Console.WriteLine("Threads all started. Setting signal now.");
_signal.Set();
// Thread.Sleep(50); // With no sleep at all, NO threads receive the signal.
_signal.Reset();
Thread.Sleep(1000);
Console.WriteLine("\n{0}/{1} threads received the signal.\n\n", _signalledCount, NUM_THREADS);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static void test(int id)
{
Console.WriteLine("Thread " + id + " started.");
_startCounter.Signal();
_signal.Wait();
Interlocked.Increment(ref _signalledCount);
Console.WriteLine("Task " + id + " received the signal.");
}
private const int NUM_THREADS = 20;
private static readonly ManualResetEventSlim _signal = new ManualResetEventSlim();
private static CountdownEvent _startCounter;
private static int _signalledCount;
}
}
Note: This question poses a similar problem, but it doesn't seem to have an answer (other than confirming that yes, this can happen).
Issue with ManualResetEvent not releasing all waiting threads consistently
[EDIT]
As Ian Griffiths points out below, the answer is that the underlying Windows API that is used is not designed to support this.
It's unfortunate that the Microsoft documentation for ManualResetEventSlim.Set() states wrongly that it
Sets the state of the event to signaled, which allows one or more threads waiting on the event to proceed.
Clearly "one or more" should be "zero or more".
ThreadPool.SetMinThreads()
hack. ;) – Pollux