Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

How to avoid ThreadPool bottleneck when it runs out of threads

We have an application with some time constrains, say that we need to execute an action every 500 ms, it is a kind of watchdog so if we don't send a message before 500 ms bad things happen.

The application uses quite heavily the ThreadPool and this watchdog thing interacts with the ThreadPool.

We have found that on some low end machines and sometimes when we queue a new workitem it takes about 800ms to execute it so the watchdog fires. We guess it is related with the ThreadPool running out of threads / creating new ones.

Is there a way to avoid this like forcing the ThreadPool to create the threads in advance or in a different thread so the watchdog never has to wait until the ThreadPool can execute the request?

Upvotes: 0

Views: 1760

Answers (2)

Roger Johansson
Roger Johansson

Reputation: 23214

The reply from lawliet29 can help to some extent. But even with many threads, the thread pool can become saturated and some tasks might be queued up at the end of the global task queue.

We have the same issue inside Akka.NET where we have system actors that must execute even under heavy load.

We are now moving those sensitive tasks into it's own dedicated threadpool, so that those tasks will not end up on the end of the global pool queue.

https://github.com/helios-io/DedicatedThreadPool

We can schedule tasks onto this queue using a special task scheduler.

Upvotes: 3

lawliet29
lawliet29

Reputation: 355

You can try using ThreadPool.SetMinThreads method.

Upvotes: 1

Related Questions