linux最多能同时运行几个线程?

bbisonic bbisonic 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了程序如下:
结果创建到第306个线程时就报如下错误:
can't create thread 306: Cannot allocate memory
can't create thread 307: Cannot allocate memory
谁能帮忙解释下为什么吗?
难道同时只能运行305个线程么?

#include <stdio.h>
#include <pthread.h>
#include <string.h>

pthread_t ntid;

void *
thr_fn(void *arg)
{
pthread_detach(pthread_self());

sleep(50000);
}

#define MAXTHREAD 600

int
main(void)
{
int err;
int i;

for (i = 0; i < MAXTHREAD; i++)
{
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != 0)
fprintf(stderr, "can't create thread %d: %s\n", i + 1, strerror(err));
else
printf("thread %d create success\n", i + 1);
}

sleep(500000);
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
bbisonic
Linux/Unix社区 23,224 成员 74,535 内容