I am trying to understand what ThreadPool does, I have this .NET example:
class Program
{
static void Main()
{
int c = 2;
// Use AutoResetEvent for thread management
AutoResetEvent[] arr = new AutoResetEvent[50];
for (int i = 0; i < arr.Length; ++i)
{
arr[i] = new AutoResetEvent(false);
}
// Set the number of minimum threads
ThreadPool.SetMinThreads(c, 4);
// Enqueue 50 work items that run the code in this delegate function
for (int i = 0; i < arr.Length; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
Thread.Sleep(100);
arr[(int)o].Set(); // Signals completion
}, i);
}
// Wait for all tasks to complete
WaitHandle.WaitAll(arr);
}
}
Does this run 50 “tasks”, in groups of 2 (int c) until they all finish? Or I am not understanding what it really does.
By setting the minimum number of threads, the only thing you’re asking the .NET runtime to do is to please allocate at least 2 threads for the thread pool. You’re not asking it to limit itself to only 2.
As such, there is no guarantee on how many threads in particular your program will use for this. It depends on your system and a lot of other factors.
A simple test I made (minor change to your program to just keep track of simultaneous threads entering the sleep call) maxed out at 4 in one run, 3 in another, 7 in another, 10 in another, etc.
You really shouldn’t need to change the thread pool size.
What are you trying to accomplish?