Default values for System.Threading.ThreadPool.SetMaxThreads

别来无恙 提交于 2019-12-03 15:22:53

问题


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

System.Threading.ThreadPool.SetMaxThreads

What are the default values?


回答1:


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.




回答2:


There is one thread pool per process. 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. The number of threads in the thread pool can be changed by using the SetMaxThreads method. Each thread uses the default stack size and runs at the default priority. Blockquote

Unmanaged code that hosts the .NET Framework can change the size of the thread pool by using the CorSetMaxThreads function, defined in the mscoree.h file.




回答3:


It is not a fixed number, it is dependant on available memory and other factors - you can find it at runtime by using GetMaxThreads()



来源:https://stackoverflow.com/questions/10947700/default-values-for-system-threading-threadpool-setmaxthreads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!