We have a situation where we are spawning multiple threads to do some file copy operations using ThreadPool.QueueUserWorkItem() API from a for loop of items in a job queue. The job queue contains around 50 items(used for testing purposes).
We were expecting a maximum no. of 50 threads getting spawned for processing or even lesser when the system cannot spare as many threads(using a higher work load). But when we printed the no. of available threads in the pool using ThreadPool.GetAvailableThreads(out _iWorkerThreads, out _ioThreads) we are getting a constant 1023(for job size 50 for worker threads) and no data for I/O threads.
Not sure why so many threads are getting spawned. Is the output from ThreadPool.GetAvailableThreads reliable? Would it help to set the max thread count in the thread pool? What is a reliable way to evaluate the average no. of managed threads being used by the process from the pool?
Can anyone point me why the CPU utilization is spiking to almost 100% when the app is running. The code uses WaitHandles, typically ManualResetEvents, attached to each job for signalling the main thread for job completion.
That's not the number of spawned threads. That's the number of additional threads that could be spawned before the maximum is reached.
MSDN: ThreadPool.GetAvailableThreads Method
GetMaxThreads minus GetAvailableThreads is probably the number you are looking for.
Without doing anything:
class Program
{
static void Main(string[] args) {
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine(workerThreads);
Console.WriteLine(completionPortThreads);
Console.ReadLine();
}
}
The output is:
1023
1000
If you make sure to call ThreadPool.GetAvailableThreads while you know some of your threads are still working, the number should be lower.
Example:
static void Main(string[] args) {
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine(workerThreads);
Console.WriteLine(completionPortThreads);
Console.WriteLine();
ThreadPool.QueueUserWorkItem(o => Thread.Sleep(10000));
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine(workerThreads);
Console.WriteLine(completionPortThreads);
Console.ReadLine();
}
The output is:
1023
1000
1022
1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With