我的电脑每个进程允许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);
}