Linux pthread_create 可以创建的最大线程数是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12387828/
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 that pthread_create can create?
提问by injoy
Possible Duplicate:
The thread create by pthread_create the same with the kernel thread?
I use the code below to test the maximum number of threads that the pthread_create function can create.
我使用下面的代码来测试 pthread_create 函数可以创建的最大线程数。
#include <pthread.h>
#include <stdio.h>
static unsigned long long thread_nr = 0;
pthread_mutex_t mutex_;
void* inc_thread_nr(void* arg) {
(void*)arg;
pthread_mutex_lock(&mutex_);
thread_nr ++;
pthread_mutex_unlock(&mutex_);
/* printf("thread_nr = %d\n", thread_nr); */
sleep(300000);
}
int main(int argc, char *argv[])
{
int err;
int cnt = 0;
pthread_t pid[1000000];
pthread_mutex_init(&mutex_, NULL);
while (cnt < 1000000) {
err = pthread_create(&pid[cnt], NULL, (void*)inc_thread_nr, NULL);
if (err != 0) {
break;
}
cnt++;
}
pthread_join(pid[cnt], NULL);
pthread_mutex_destroy(&mutex_);
printf("Maximum number of threads per process is = %d\n", thread_nr);
}
And the output is :
输出是:
Maximum number of threads per process is = 825
Is that the maximum number of threads that the pthread_create function can create?
这是 pthread_create 函数可以创建的最大线程数吗?
Besides, I use the command below to view the maximum number of threads my system allows:
此外,我使用以下命令查看系统允许的最大线程数:
# cat /proc/sys/kernel/threads-max
And the number is 772432.
号码是772432。
Why is the output of my program not equal to the value of threads-max
?
为什么我的程序的输出不等于 的值threads-max
?
My OS is Fodaro 16, with 12 cores, 48G RAM.
我的操作系统是 Fodaro 16,12 核,48G RAM。
采纳答案by Nominal Animal
The default size for the per-thread stack is artificially imposing the limit in your test. While the default stack given to the process (the initial thread) grows dynamically as needed, the stacks for the other threads are fixed in size. The default size is usually extremely large, something like two megabytes, to make sure the per-thread stack is large enough for even the pathological cases (deep recursion and so on).
每个线程堆栈的默认大小是人为地在测试中强加限制。虽然提供给进程(初始线程)的默认堆栈根据需要动态增长,但其他线程的堆栈大小是固定的。默认大小通常非常大,例如 2 兆字节,以确保每个线程堆栈足够大,即使是病理情况(深度递归等)。
In most cases, thread workers need very little stack. I've found that on all architectures I've used, 64k (65536 bytes) per-thread stack is sufficient, as long as I don't use deeply recursive algorithms or large local variables (structures or arrays).
在大多数情况下,线程工作者只需要很少的堆栈。我发现在我使用过的所有架构上,只要我不使用深度递归算法或大型局部变量(结构或数组),每个线程堆栈 64k(65536 字节)就足够了。
To explicitly specify a per-thread stack size, modify your main()
to something like the following:
要明确指定每个线程的堆栈大小,请将您main()
的内容修改为如下所示:
#define MAXTHREADS 1000000
#define THREADSTACK 65536
int main(int argc, char *argv[])
{
pthread_t pid[MAXTHREADS];
pthread_attr_t attrs;
int err, i;
int cnt = 0;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, THREADSTACK);
pthread_mutex_init(&mutex_, NULL);
for (cnt = 0; cnt < MAXTHREADS; cnt++) {
err = pthread_create(&pid[cnt], &attrs, (void*)inc_thread_nr, NULL);
if (err != 0)
break;
}
pthread_attr_destroy(&attrs);
for (i = 0; i < cnt; i++)
pthread_join(pid[i], NULL);
pthread_mutex_destroy(&mutex_);
printf("Maximum number of threads per process is %d (%d)\n", cnt, thread_nr);
}
Note that attrs
is not consumed by the pthread_create()
call. Think of the thread attributes more like a template on how pthread_create()
should create the threads; they are not attributes given to the thread. This trips up many aspiring pthreads programmers, so it's one of those things you'd better get right from the get go.
请注意,attrs
它不会被pthread_create()
调用消耗。把线程属性想象成一个关于如何pthread_create()
创建线程的模板;它们不是赋予线程的属性。这让许多有抱负的 pthreads 程序员绊倒了,所以这是你最好从一开始就做好的事情之一。
As to the stack size itself, it must be at least PTHREAD_STACK_MIN
(16384 in Linux, I believe) and divisible by sysconf(_SC_PAGESIZE)
. Since page size is a power of two on all architectures, using a large enough power of two should always work.
至于堆栈大小本身,它必须至少是PTHREAD_STACK_MIN
(我相信在 Linux 中为 16384)并且可以被sysconf(_SC_PAGESIZE)
. 由于页面大小在所有架构上都是 2 的幂,因此使用足够大的 2 的幂应该始终有效。
Also, I added a fix in there, too. You only try to join a nonexistent thread (the one that the loop tried to create, but failed), but you need to join all of them (to make sure they're all done their job).
另外,我也在那里添加了一个修复程序。您只尝试加入一个不存在的线程(循环尝试创建但失败的线程),但您需要加入所有线程(以确保它们都完成了工作)。
Further recommended fixes:
进一步推荐的修复:
Instead of using a sleep, use a condition variable. Have each thread wait (pthread_cond_wait()
) on the condition variable (while holding the mutex), then release the mutex and exit. That way your main function only needs to broadcast (pthread_cond_broadcast()
) on the condition variable to tell all threads they can now exit, then it can join each one, and you can be sure that that number of threads were really concurrently running. As your code stands now, some threads may have enough time to wake up from the sleep and exit.
不要使用睡眠,而是使用条件变量。让每个线程pthread_cond_wait()
在条件变量上等待 ( )(同时持有互斥锁),然后释放互斥锁并退出。这样你的主函数只需要pthread_cond_broadcast()
在条件变量上广播 ( ) 来告诉所有线程它们现在可以退出,然后它可以加入每个线程,并且你可以确定该数量的线程确实在并发运行。就您现在的代码而言,某些线程可能有足够的时间从睡眠中唤醒并退出。
回答by phsym
According to pthread_create(3)
man page, there is another limit :
根据pthread_create(3)
手册页,还有另一个限制:
RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number
of process for a real user ID.
Try to find the value of this limit with getrlimit(2)
. If the value still does not match the number you measured (825), try to change this limit with setrlimit(2)
to verify if it affects your measurements.
尝试使用 找到此限制的值getrlimit(2)
。如果该值仍然与您测量的数字 (825) 不匹配,请尝试更改此限制setrlimit(2)
以验证它是否会影响您的测量。
EDIT:In fact the RLIMIT_NPROC limit value is the same that the one obtained with the shell command ulimit -u
(that prints / set the max user processes).
编辑:实际上 RLIMIT_NPROC 限制值与使用 shell 命令ulimit -u
(打印/设置最大用户进程)获得的值相同。
回答by P.P
In theory, there's no limit on the number of threads a process can have. But the practical restriction could come from the fact that all threads share the resources.
理论上,进程可以拥有的线程数没有限制。但实际限制可能来自所有线程共享资源的事实。
It means at some point, a process can't create more than a certain number of processes due to lack resources to share with such stack space, for example.
这意味着在某些时候,例如,由于缺乏与此类堆栈空间共享的资源,进程无法创建超过一定数量的进程。