Define current size of thread pool in .NET

1.1k views Asked by At

I want to know the number of active threads (iocp and worker) in threadpool. I use this technique:

ThreadPool.GetAvailableThreads(out var workerAvailable, out var iocpAvailable);
ThreadPool.GetMinThreads(out var workerMin, out var iocpMin);
ThreadPool.GetMaxThreads(out var workerMax, out var iocpMax);

var total = Process.GetCurrentProcess().Threads.Count;
var workerCurrent = workerMax - workerAvailable;
var iocpCurrent = iocpMax - iocpAvailable;
var other = total - worker - iocp;

There are quite strange numbers in logs: 8 worker, 3 iocp and 150 other threads.

I made a dump using procdump tool and inspected it using ClrMd (ClrThread.IsThreadpoolCompletionPort and ClrThread.IsThreadpoolWorker properties). Finally I got a different result from the dump: 99 worker and 14 iocp threads.

Why does the first approach return such strange result?

UPD: I suppose ThreadPool.GetAvailableThreads returns max minus currently active (not idle) threads.

0

There are 0 answers