I created my thread pool as follows:
ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(20, 20);
However, my application started to hang under heavy load. This was due to the fact that work tasks were not performed: I used ThreadPool.QueueUserWorkItemto run some tasks, which, in turn, used the same method for further work in the queue. This is obviously dangerous for a limited thread pool (deadlock situation), but I use a thread pool to not limit the maximum threads, but to reduce the cost of creating threads.
I see a potential trap there, but I figured that setting the maximum number of 10000 threads in the pool means that if the item was queued, all threads were busy and there were no 10000 threads in the pool, a new one will be created and the task will be processed there.
However, I changed this:
ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(200, 200);
.. and the application started to work. If this made it start working, am I missing something about how / when the thread pool expands from minimum to maximum size?
source
share