ThreadPool max threads

56,228

Solution 1

From the MSDN :

When demand is low, the actual number of thread pool threads can fall below the minimum values.

Read this too: Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4

Solution 2

Firstly, your "knowledge" of the defaults is incorrect. The limit of 25 threads per processor was back from .NET 1.1. It was increased in .NET 2, and now:

Beginning with the .NET Framework version 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. A process can call the GetMaxThreads method to determine the number of threads.

However, there's something else at play: the thread pool doesn't immediately create new threads in all situations. In order to cope with bursts of small tasks, it limits how quickly it creates new threads. IIRC, it will create one thread every 0.5 seconds if there are outstanding tasks, up to the maximum number of threads. I can't immediately see that figure documented though, so it may well change. I strongly suspect that's what you're seeing though. Try queuing a lot of items and then monitor the number of threads over time.

Solution 3

Firstly check this link, especially this remark:

If the common language runtime is hosted, for example by Internet Information Services (IIS) or SQL Server, the host can limit or prevent changes to the thread pool size.

Then you should check the return value of ThreadPool.SetMaxThreads(threads, threads) method. Maybe it returns false?

Share:
56,228
foxy
Author by

foxy

Updated on September 22, 2020

Comments

  • foxy
    foxy over 3 years

    I've got some trouble with .NET's ThreadPool (.NET 4).

    I've read that by default .NET has a limit of 25 threads per processor, but according to forum posts on SO and on other places, I can increase the limit with the below code.

    void SetThreads(int threads)
    {
        ThreadPool.SetMaxThreads(threads, threads);
        ThreadPool.SetMinThreads(threads, threads);
    }
    

    However, when I set the above to some arbitrarily high number, for example, 2000, and queue ~1000 items, I still only have ~33 threads running (.NET CLR takes ~5 threads), and ThreadPool.GetAvailableThreads() returns 1971 threads remaining.

    Why doesn't the code above work?

  • foxy
    foxy about 13 years
    Thank you, I'm using .NET 4 on Windows 7 though.
  • foxy
    foxy about 13 years
    I queued ~50k tasks and set the ThreadPool's min + max threads to 20k, yet only ~25 threads were used. Is this expected?
  • Emond
    Emond about 13 years
    Did any/the tasks complete or get in a wait state? It is up to the manager to create threads. On some machines it might be better to not have a lot of threads because the threads will suffer from thread starvation.
  • Fayilt
    Fayilt over 10 years
    This should be the accepted answer, it clearly explains what freedompeace is experiencing
  • ygoe
    ygoe over 10 years
    That was the answer! I noticed that strange half second, sometimes one second where it did nothing at all. I have waiting Tasks and other Tasks that wait to be started in consequence, and everything is slow. I need about 10 threads for my work. Setting ThreadPool.SetMinThreads to like 20 (default was 8) worker threads fixed my issue immediately! Since I need those threads later as well, I won't decrease the minimum again.