Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values for System.Threading.ThreadPool.SetMaxThreads

Tags:

Suppose, I don't set any values explicitly by calling the function:

System.Threading.ThreadPool.SetMaxThreads 

What are the default values?

like image 225
Shamim Hafiz - MSFT Avatar asked Jun 08 '12 11:06

Shamim Hafiz - MSFT


People also ask

What is the default value for thread pool size?

The size for the default thread pool is set to 10 minimum and 10 maximum threads. A timeout value is set to 3500 milliseconds.

What is a ThreadPool thread C#?

Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.

What is the use of ThreadPool QueueUserWorkItem method in C#?

QueueUserWorkItem(WaitCallback, Object) Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.

How do I limit the number of threads in C#?

To set the lower limit of the threads in the thread pool you can use the ThreadPool. SetMinThreads property. The default lower limit of the number of threads in the thread pool is one thread per processor.


1 Answers

It depends on the .NET framework version, changed in 2.0, 3.0 and 4.0. In 2.0 it was 50 times the number of cores. In 3.0 (aka 2.0 SP1) it was 250 times the number of cores, 4.0 made it dynamic depending on bitness and OS resources. Max I/O completion threads was always 1000 if I remember correctly.

In general, it is insanely high and a program should never get close. On a 32-bit machine, the program is pretty likely to bomb with OOM first when all of those threads consume the available virtual memory with their one megabyte stacks. In general, it can only get out of hand when there are a lot of TP thread requests and the running ones are not completing for minutes. The ideal for a TP thread is to not take more than half a second.

The Debug > Windows > Threads debugger window tells the unpleasant truth. And gives a very good hint why these TP threads are not completing, you can see their call stack.

like image 102
Hans Passant Avatar answered Sep 29 '22 08:09

Hans Passant