Reputation: 31
My computer allows 380 threads per process, which is fine for me. I have no problem when I call 380 times to function sdfpthread_create (). But successive call returns the erro 11 (Resource temporarily unavailable).
The apparent solution is to use the pthread_exit (), but I did not solve the problem, the limit is still 380 threads created.
How I can reuse thread?
#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);
}
Upvotes: 2
Views: 637
Reputation: 84151
You need to call pthread_join(3)
to actually cleanup thread exit state, or create threads that are not joinable.
Please see and read:
Upvotes: 4