vote buttons
1
1
advanced
0
beginner
0
intermediate
02-Nov-2014 02:45 UTC
K Bonneau
350

1 Answers

vote buttons
1

Lets first understand what is the MinThreads setting of a thread pool. ThreadPool starts with a set number of threads pre created. After that as we keep assigning work to the thread pool, it checks if there are any free thread available, if not it waits for around half a second for some thread to become free. If no thread becomes free, it will create a new Thread. It will keep creating new threads till a certain maximum number is reached. The initial number of pre-created threads is the MinThreads and the maximum number of threads it can create is the MaxThreads setting of a thread pool. Lets see what is the default minimum number of threads:

int minWorkerThreads;
int minIOThreads;
ThreadPool.GetMinThreads(out minWorkerThreads, out minIOThreads);

The minimum threads matches the number of cores on the machine it runs under. If you run the above code in a dual core machine the values for minWorkerThreads and minIOThreads would be equal to 2. 

Note: On server environments the minimum value is much higher than the number of cores.

Now lets say I have a initial burst of activity as soon as the code starts running and each activity is delegated to the thread pool. Each activity waits for some IO in between. Lets say I have 100 such activities. If the machine is a dual core machine the min threads set for the thread pool is 2. So the first two activities get assigned quickly. Now when the third activity is assigned there are no free threads, so the thread pool will wait for half a second before it creates a new thread and the work starts only then. Similarly for other threads. This will give us a very poor performance as we have to wait half a second for each thread to be created after the first two threads. If we are able to change the min threads to 100, our problem would be solved. Thankfully .Net provides a method SetMinThreads for exactly this purpose. We can use it as following:

ThreadPool.SetMinThreads (100, 100);
02-Nov-2014 03:38 UTC
Rahul
100