C++ 一个进程在windows中可以拥有的最大线程数是多少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16710849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the maximum number of threads a process can have in windows
提问by CodeRider
In a windows process is there any limit for the threads to be used at a time. If so what is the maximum number of threads that can be used per process?
在 Windows 进程中,一次使用的线程是否有任何限制。如果是这样,每个进程可以使用的最大线程数是多少?
采纳答案by rodrigo
There is no limit that I know of, but there are two practical limits:
我知道没有限制,但有两个实际限制:
- The virtual space for the stacks. For example in 32-bits the virtual space of the process is 4GB, but only about 2G are available for general use. By default each thread will reserve 1MB of stack space, so the top value are 2000 threads. Naturally you can change the size of the stack and make it lower so more threads will fit in (parameter
dwStackSize
inCreateThread
or option/STACK
in the linker command). If you use a 64-bits system this limit practically dissapears. - The scheduler overhead. Once you read the thousands of threads, just scheduling them will eat nearly 100% of your CPU time, so they are mostly useless anyway. This is not a hard limit, just your program will be slower and slower the more threads you create.
- 堆栈的虚拟空间。比如32位进程的虚拟空间是4GB,但是一般只有2G左右。默认情况下,每个线程将保留 1MB 的堆栈空间,因此最高值为 2000 个线程。自然地,您可以更改堆栈的大小并使其变小,以便容纳更多线程(参数
dwStackSize
inCreateThread
或/STACK
链接器命令中的选项)。如果您使用 64 位系统,则此限制实际上会消失。 - 调度程序开销。一旦您阅读了数千个线程,仅仅调度它们将占用您近 100% 的 CPU 时间,因此无论如何它们大多是无用的。这不是硬性限制,只是您创建的线程越多,您的程序就会越来越慢。
回答by Mats Petersson
The actual limit is determined by the amount of available memory in various ways. There is no limit of "you can't have more than this many" of threads or processes in Windows, but there are limits to how much memory you can use within the system, and when that runs out, you can't create more threads.
实际限制由可用内存量以各种方式确定。在 Windows 中“您不能拥有超过这么多”的线程或进程没有限制,但是您可以在系统内使用多少内存是有限制的,当内存用完时,您无法创建更多线程。
See this blog by Mark Russinovich: http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx
请参阅 Mark Russinovich 的此博客:http: //blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx