问题描述
下面的代码应该可以创建100,000个线程:
The following code is supposed to make 100,000 threads:
/* compile with: gcc -lpthread -o thread-limit thread-limit.c */
/* originally from: http://www.volano.com/linuxnotes.html */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define MAX_THREADS 100000
int i;
void run(void) {
sleep(60 * 60);
}
int main(int argc, char *argv[]) {
int rc = 0;
pthread_t thread[MAX_THREADS];
printf("Creating threads ...\n");
for (i = 0; i < MAX_THREADS && rc == 0; i++) {
rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
if (rc == 0) {
pthread_detach(thread[i]);
if ((i + 1) % 100 == 0)
printf("%i threads so far ...\n", i + 1);
}
else
{
printf("Failed with return code %i creating thread %i (%s).\n",
rc, i + 1, strerror(rc));
// can we allocate memory?
char *block = NULL;
block = malloc(65545);
if(block == NULL)
printf("Malloc failed too :( \n");
else
printf("Malloc worked, hmmm\n");
}
}
sleep(60*60); // ctrl+c to exit; makes it easier to see mem use
exit(0);
}
这是在具有32GB RAM的64位计算机上运行的;已安装Debian 5.0,所有库存.
This is running on a 64bit machine with 32GB of RAM; Debian 5.0 installed, all stock.
- ulimit -s 512以减小堆栈大小
- /proc/sys/kernel/pid_max设置为1,000,000(默认情况下,上限为32k pid).
- ulimit -u 1000000以增加最大进程(一点也不重要)
- /proc/sys/kernel/threads-max设置为1,000,000(默认情况下,完全没有设置)
运行此命令会吐出以下内容:
Running this spits out the following:
65500 threads so far ...
Failed with return code 12 creating thread 65529 (Cannot allocate memory).
Malloc worked, hmmm
我当然不会用完公羊.我什至可以启动多个同时运行的程序,它们都启动其65k线程.
I'm certainly not running out of ram; I can even launch several more of these programs all running at the same time and they all start their 65k threads.
((请避免建议我不要尝试启动100,000个以上的线程.这是对应该可以正常工作的测试).我当前的基于epoll的服务器始终具有大约200k +的连接,而各种论文都表明线程可能只是一个更好的选择--谢谢:))
(Please refrain from suggesting I not try to launch 100,000+ threads. This is simple testing of something which should work. My current epoll-based server has roughly 200k+ connections at all times and various papers would suggest that threads just might be a better option. - Thanks :) )
推荐答案
pilcrow对/proc/sys/vm/max_map_count
的提及是正确的;增大该值将允许打开更多线程;不确定所涉及的确切公式,但是1mil +的值允许大约300k +线程.
pilcrow's mention of /proc/sys/vm/max_map_count
is right on track; raising this value allows more threads to be opened; not sure of the exact formula involved, but a 1mil+ value allows for some 300k+ threads.
(对于尝试使用100k +线程的其他人,请查看 pthread_create的mmap 问题...当内存不足时,使新线程变得非常慢.)
(For anyone else experimenting with 100k+ threads, do look at pthread_create's mmap issues... making new threads gets really slow really fast when lower memory is used up.)
这篇关于NPTL将最大线程数限制为65528吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!