我的计算机允许每个进程 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);
}
最佳答案
关于c - 如何使用 pthread_exit() 重用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691439/