【发布时间】:2013-02-04 16:52:29
【问题描述】:
我的计算机允许每个进程有 380 个线程,这对我来说很好。我没有问题 当我调用 380 次函数 sdfpthread_create() 时。但连续调用返回 错误 11(资源暂时不可用)。
明显的解决方法是使用pthread_exit(),但是我没有解决问题, 限制仍然是创建 380 个线程。
如何重用线程?
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
void *doSomeThing()
{
sleep(99);
pthread_exit(NULL);
}
int main(void)
{
pthread_t tid;
int i;
int err;
/* Create threads */
for (i=0; i<380; ++i) {
err = pthread_create(&tid, NULL, doSomeThing, NULL);
if (err != 0)
printf("\n1) Can't create thread :[%s]", strerror(err));
}
sleep(1);
/* Reuse threads */
for (i=0; i<5; ++i) {
err = pthread_create(&tid, NULL, doSomeThing, NULL);
if (err != 0)
printf("\n2) Can't create thread :[%s]", strerror(err));
}
exit(0);
}
【问题讨论】:
-
这不是很清楚。你是说上面的代码有效还是无效?