简体   繁体   English

C ++应用程序可以创建多少个线程

[英]How many threads can a C++ application create

I'd like to know, how many threads can a C++ application create at most. 我想知道,C ++应用程序最多可以创建多少个线程。 Does OS, hardware caps and other factors influence on these bounds? 操作系统,硬件上限和其他因素是否会影响这些界限?

[C++11: 1.10/1]: [..] Under a hosted implementation, a C++ program can have more than one thread running concurrently. [C++11: 1.10/1]: [..]在托管实现中,C ++程序可以同时运行多个线程。 [..] Under a freestanding implementation, it is implementation-defined whether a program can have more than one thread of execution. [..]在一个独立的实现中,它是实现定义的,一个程序是否可以有多个执行线程。

[C++11: 30.3/1]: 30.3 describes components that can be used to create and manage threads. [C++11: 30.3/1]: 30.3描述了可用于创建和管理线程的组件。 [ Note: These threads are intended to map one-to-one with operating system threads. [注意: 这些线程旨在与操作系统线程一对一映射。 —end note ] - 尾注]

So, basically, it's totally up to the implementation & OS; 所以,基本上,它完全取决于实现和操作系统; C++ doesn't care! C ++不关心!

It doesn't even list a recommendation in Annex B "Implementation quantities"! 它甚至没有列出附件B“实施数量”中的建议 (which seems like an omission, actually). (实际上这似乎是一个遗漏)。

C++ as language does not specify a maximum (or even a minimum beyond the one). C ++作为语言没有指定最大值(甚至超过一个最小值)。 The particular implementation can, but I never saw it done directly. 特定的实现可以,但我从未看到它直接完成。 The OS also can, but normally just states a lank like limited by system resources. 操作系统也可以,但通常只是表示受系统资源限制的lank。 Each thread uses up some nonpaged memory, selector tables, other bound things, so you may run out of that. 每个线程都会占用一些非分页内存,选择器表和其他绑定内容,因此您可能会用完它。 If you don't the system will become pretty unresponsive if the threads actually do work. 如果不这样做,如果线程确实有效,系统将变得非常无响应。

Looking from other side, real parallelism is limited by actual cores in the system, and you shall not have too many threads. 从另一方面来看,真正的并行性受到系统中实际内核的限制,并且您不会有太多线程。 Applications that could logically spawn hundreds or thousands usually start using thread pools for good practical reasons. 出于良好的实际原因,可能在逻辑上产生数百或数千的应用程序通常会开始使用线程池。

Basically, there are no limits at your C++ application level . 基本上,您的C ++ 应用程序级别 没有限制 The number of maximum thread is more on the OS level (based on your architecture and memory available). 操作系统级别上的最大线程数量更多(基于您的体系结构和可用内存)。

On Linux , there are no limit on the maximum number of thread per process. 在Linux上 ,每个进程的最大线程数没有限制。 The number of thread is limited system wide. 线程数量受系统限制。 You can check the number of maximum allowed threads by doing: 您可以通过执行以下操作来检查允许的最大线程数:

cat /proc/sys/kernel/threads-max

On Windows you can use the testlimit tool to check the maximum number of thread: http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx 在Windows上,您可以使用testlimit工具检查最大线程数: http//blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx

On Mac OS , please read this table to find the number of thread based on your hardware configuration 在Mac OS上 ,请阅读此表以根据您的硬件配置查找线程数

However, please keep in mind that you are on a multitasking system . 但是,请记住,您使用的是多任务处理系统 The number of threads executed at the same time is limited by the total number of processor cores available . 同时执行的线程数受可用处理器核心总数的限制 To do more things, the system tries to switch between all theses thread. 为了做更多的事情,系统试图在所有这些线程之间切换。 Each "switch" has a performce (a few milliseconds). 每个“开关”都有一个执行(几毫秒)。 If your system is "switching" too much, it won't speed too much time to "work" and your overall system will be slow. 如果你的系统“切换”太多,它将不会加快太多时间“工作”,你的整个系统将会很慢。

Generally, the limit of number of threads is the amount of memory available, but there have been systems around that have lower limits. 通常,线程数的限制是可用的内存量,但是有一些系统具有较低的限制。

Unless you go mad with creating threads, it's very unlikely it will be a problem to have a limit. 除非你疯狂地创建线程,否则限制是不可能的。 Creating more threads is rarely beneficial, once you reach a certain number - that number may be around the same as, or a few times higher than, the number of cores (which for real big, heavy hardware can be a few hundred these days, with 16-core processors and 8 sockets). 一旦达到一定数量,创建更多线程很少有益 - 这个数字可能与核心数量大致相同或高几倍(对于真正的大型,重型硬件,这些天数可能是几百个,配备16核处理器和8个插槽)。

Threads that are CPU bound should not be more than the number of processors - nothing good comes from that. CPU绑定的线程不应超过处理器的数量 - 没有什么好处。

Threads that are doing I/O or otherwise "sitting around waiting" can be higher in numbers - 2-5 per processor core seems reasonable. 正在进行I / O或以其他方式“等待”的线程数量可能更高 - 每个处理器核心2-5个似乎是合理的。 Given that modern machines have 8 sockets and 16 cores at the higher end of the spectrum, that's still only around 1000 threads. 鉴于现代机器在频谱的较高端有8个插座和16个核心,那仍然只有大约1000个线程。

Sure, it's possible to design, say, a webserver system where each connection is a thread, and the system has 10k or 20k connections active at any given time. 当然,可以设计一个网络服务器系统,其中每个连接都是一个线程,系统在任何给定时间都有10k或20k连接。 But it's probably not the most efficient. 但它可能不是最有效的。

Apart from the general impracticality of having many more threads than cores, yes, there are limits. 除了拥有比核心更多线程的一般不切实际之外,是的,还有一些限制。 For example, a system may keep a unique "process ID" for each thread, and there may be only 65535 of them available. 例如,系统可以为每个线程保留唯一的“进程ID”,并且可能只有65535个可用。 Also, each thread will have its own stack, and those stacks will eventually consume too much memory (you can however adjust the size of each stack when you spawn threads). 此外,每个线程都有自己的堆栈,这些堆栈最终会消耗太多内存(但是,当您生成线程时,可以调整每个堆栈的大小)。

Here's an informative article--ignore the fact that it mentions Windows, as the concepts are similar on other common systems: http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx 这是一篇内容丰富的文章 - 忽略它提到Windows的事实,因为其他常见系统的概念类似: http//blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx

There is nothing in the C++ standard that limits number of threads. C ++标准中没有任何内容限制线程数。 However, OS will certainly have a hard limit. 但是,操作系统肯定会有一个硬限制。

Having too many threads decreases the throughput of your application, so it's recommended that you use a thread pool. 线程太多会降低应用程序的吞吐量,因此建议您使用线程池。

I'd like to know, how many threads can a C++ application create at most. 我想知道,C ++应用程序最多可以创建多少个线程。

Implementation/OS-dependent. 执行/ OS依赖。

Keep in mind that there were no threads in C++ prior to C++11. 请记住,在C ++ 11之前,C ++中没有线程。

Does OS, hardware caps and other factors influence on these bounds? 操作系统,硬件上限和其他因素是否会影响这些界限?

Yes. 是。

OS might be able limit number of threads a process can create. 操作系统可能能够限制进程可以创建的线程数。
OS can limit total number of threads running simultaneously (to prevent fork bombs, etc, linux can definitely do that). 操作系统可以限制同时运行的线程总数(为了防止fork炸弹等,linux绝对可以做到这一点)。
Available physical(and virtual) memory will limit number of threads you can create IF each thread allocates its own stack. 如果每个线程分配自己的堆栈,可用的物理(和虚拟)内存将限制您可以创建的线程数。
There can be a (possibly hardcoded) limit on how many thread "handles" OS can provide. 可能存在(可能是硬编码的)限制OS可以提供​​多少线程“句柄”。
Underlying OS/platform might not have threads at all (real-mode compiler for DOS/FreeDOS or something similar). 底层OS /平台可能根本没有线程(DOS / FreeDOS的实模式编译器或类似的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM