Windows Server 2003 中的最大线程数是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/481900/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:58:52  来源:igfitidea点击:

What's the maximum number of threads in Windows Server 2003?

windowsmultithreading

提问by Douglas Anderson

Does anyone know? And a bigger question is what happens when you encounter this maximum? Is this the same number with other Windows OSs such as Vista, XP etc.?

有人知道吗?一个更大的问题是当你遇到这个最大值时会发生什么?这是否与其他 Windows 操作系统(如 Vista、XP 等)相同?

回答by ShuggyCoUk

First I would advise reading this: http://blogs.msdn.com/oldnewthing/archive/2007/03/01/1775759.aspx

首先我建议阅读这个:http: //blogs.msdn.com/oldnewthing/archive/2007/03/01/1775759.aspx

then http://blogs.msdn.com/oldnewthing/archive/2005/07/29/444912.aspx

然后http://blogs.msdn.com/oldnewthing/archive/2005/07/29/444912.aspx

To summarise, the limitation is normally stack space (which must be in contiguous blocks) and since every thread consumes this scattered about you rapidly run out of contiguous blocks. On 64 bit machines and operating systems this is much less of a problem.

总而言之,限制通常是堆栈空间(必须在连续块中),并且由于每个线程都消耗了这些分散的空间,因此您很快就会用完连续块。在 64 位机器和操作系统上,这不是一个问题。

Mitigation strategies exist but will only go so far (and rely on you not using much stack per thread)

存在缓解策略,但只会到此为止(并且依赖于每个线程不使用太多堆栈)

As a rough guide:

作为一个粗略的指南:

  • creating tens is almost certain to work
  • hundreds is probable on current server and desktop hardware but risky
  • thousands will almost certainly fail.
  • 创建十几乎肯定会起作用
  • 在当前的服务器和桌面硬件上可能有数百个,但有风险
  • 成千上万的人几乎肯定会失败。

You likely shouldn't need to create more than ten anyway (and if you really doneed to you should know this information already)

你可能不应该需要创建十余反正(如果你真的这样做需要你应该已经知道这个信息)

回答by Rex M

The best answer I've heard when asking such questions is:

在提出此类问题时,我听到的最佳答案是:

It doesn't matter, and if you find that it does matter, you need to rethink what you're doing so that it doesn't matter.

没关系,如果你发现它很重要,你需要重新考虑你在做什么,这样它就无关紧要了。

回答by TheSoftwareJedi

Note that you should examine your design closely if you are concerned about hitting this limit!!!!!!!!

请注意,如果您担心达到此限制,您应该仔细检查您的设计!!!!!!!!

The answer to your "More Important Question" of what happens is OutOfMemoryException.

关于发生的事情的“更重要的问题”的答案是 OutOfMemoryException。

Not exactly a direct answer, but here's some code to find out the limit. It could be available memory dependent though. Would be interested in seeing other OS/cpu/mem results.

不完全是直接的答案,但这里有一些代码可以找出限制。不过,它可能取决于可用内存。有兴趣查看其他 OS/cpu/mem 结果。

Feel free to edit and add your machine in:

随意编辑和添加您的机器:

  • Windows 7, VS2008, dual core, 2gb mem: 1,465 then crash with OutOfMemoryException

        int i = 0;
        try
        {
            while (true)
            {
                new Thread(new ThreadStart(() => Thread.Sleep(int.MaxValue))).Start();
                i++;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(i);
            Console.WriteLine(ex.ToString());
        }
    
  • Windows 7、VS2008、双核、2gb 内存:1,465 然后因 OutOfMemoryException 而崩溃

        int i = 0;
        try
        {
            while (true)
            {
                new Thread(new ThreadStart(() => Thread.Sleep(int.MaxValue))).Start();
                i++;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(i);
            Console.WriteLine(ex.ToString());
        }
    

回答by Michael Burr

Do read the Raymond Chen blog postings that ShuggyCoUk's answerpointed to.

请阅读ShuggyCoUk 的回答指向的 Raymond Chen 博客帖子。

But pay special attention to this bit:

但要特别注意这一点:

But the real question that is raised whenever somebody asks, "What's the maximum number of threads that a process can create?" is "Why are you creating so many threads that this even becomes an issue?"

The "one thread per client" model is well-known not to scale beyond a dozen clients or so. If you're going to be handling more than that many clients simultaneously, you should move to a model where instead of dedicating a thread to a client, you instead allocate an object. (Someday I'll muse on the duality between threads and objects.) Windows provides I/O completion ports and a thread pool to help you convert from a thread-based model to a work-item-based model.

但是,每当有人问“一个进程可以创建的最大线程数是多少?”时,就会提出一个真正的问题。是“你为什么要创建这么多线程,这甚至会成为一个问题?”

众所周知,“每个客户端一个线程”模型不会超过十几个客户端左右。如果您要同时处理多个客户端,您应该转向一个模型,而不是将线程专用于客户端,而是分配一个对象。(总有一天我会思考线程和对象之间的二元性。)Windows 提供 I/O 完成端口和线程池来帮助您从基于线程的模型转换为基于工作项的模型。

回答by Teoman shipahi

Default stack size is 1MB and the user-mode address space assigned to the windows process under 32 bit Windows OS is about 2 GB. that allow around 2000 thread per process (2000 * 1MB = 2GB). for 64 bit, practically, there is no such a problem .

默认堆栈大小为 1MB,32 位 Windows 操作系统下分配给 windows 进程的用户模式地址空间约为 2 GB。每个进程允许大约 2000 个线程(2000 * 1MB = 2GB)。对于 64 位,实际上没有这样的问题。

回答by Kosi2801

As far as I understand the whole threading model it should not have changed much since Win2K.

据我了解整个线程模型自 Win2K 以来应该没有太大变化。

There is no real limit of threads per se, but more a limit of the processes stack-space. See an in-depth explanation of threading limitsfrom Raymond Chen for more details on this.

线程本身没有真正的限制,但更多的是进程堆栈空间的限制。有关更多详细信息,请参阅Raymond Chen 对线程限制的深入解释

回答by Marco van de Voort

If you are stuck with an existing design that utilizes a high number of threads and needs to scale, you might also consider fibers:

如果您坚持使用大量线程并需要扩展的现有设计,您还可以考虑使用纤程:

http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx

It can save you a total redesign.

它可以为您节省一个全面的重新设计。

Indy considered it for Indy 10, but it never happened because the .NET adventures consumed most of the time it seems.

Indy 在 Indy 10 中考虑过它,但它从未发生过,因为 .NET 冒险似乎消耗了大部分时间。

回答by Saurabh D

Question seems very old but like to add as can be helpfull to others as well :

问题似乎很老,但喜欢补充,因为对其他人也有帮助:

This article regarding : Pushing the Limits of Windows: Processes and Threads

这篇文章关于:推动 Windows 的极限:进程和线程

http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx

http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx