Wednesday, March 10, 2010

Interesting Questions - Expert Answers Part 3

This is the question i posed to Jeffery Ritcher on 6th March 2010

Question ( Gopala ) :
As per your words i got to know that, when  CLR intializes the threadpool gets initialized with  zero threads ..Also, the the threads are created and used but they are not destroyed instead retained in the threadpool for later use.
But in some articles( For ex : In " C# Threading handbook" by Tobil Titus) i have found out that CLR Intializes the theadpool with 25 ( defalut, and we can change this value) threads
To Verify this i tried the following code on VS2010 ( .NET 4.0)

int workerThreads1, completionPortThreads1; int workerThreads2, completionPortThreads2; int workerThreads3, completionPortThreads3;

ThreadPool.GetAvailableThreads(out workerThreads1, out completionPortThreads1);
 //workerThreads1 = 1023 completionPortThreads1 = 1000

ThreadPool.GetMaxThreads(out workerThreads2, out completionPortThreads2);
// workerThreads2 = 1023 completionPortThreads2 = 1000

ThreadPool.GetMinThreads(out workerThreads3, out completionPortThreads3);
//workerThreads3 = 2 completionPortThreads3 = 2

The results seems to be contradictary.Please Advise.

Answer ( Jeffery Ritcher) :
Initially the thread pool has 0 threads in it.
However, the number of worker threads in the pool can grow to 25 per CPU (by default). 25 was the number for earlier versions of the CLR.
In .NET 4.0 the default is 1023 worker threads (instead of 25) and 1000 I/O threads.
The CLR doesn’t like to create threads but, if there are work items queued up, it will quickly create up to min threads (2) and then it will slowly create more threads up to the max threads (1023).
GetAvailableThreads returns the number of threads that the thread pool could use for doing work if work gets queued up. This number changes all the time. This number does not include thread pool threads are currently doing work but these threads could return and more thread pool threads could initate doing work and this is why the number changes all the time.

1 comment:

  1. That was an informative post. I have something equally useful, which I would like to share with those, who are concerned about heavily priced engineering books. The solution is the site that offers renting of textbooks and helps you save money and environment. I just tried. Highly recommended!