I am new to POSIX and I cant find a solution to this particular problem.
Are there any known issues with creating pthreads inside of loop with big number of iterations(>100000)?
It seems like every time I execute, it hangs on a random pthread_join
.
- I've tested for memory leaks and thread stack usage with valgrind.
- If I interrupt gdb when the program hangs it will trace the problem to
pthread_join
.
This is example code recreates my problem.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* task(void* args);
int main(int argc, char **argv)
{
int num_threads = 4;
int m = 100000;
int i;
for(i = 0; i < m; i++)
{
fprintf(stdout, "i:%d\n",i);
//parallel region starts
pthread_t threads[num_threads];
int t,rc;
for(t = 0; t < num_threads; t++)
{
rc = pthread_create(&threads[t],NULL,task,NULL);
if(rc){
fprintf(stderr,"ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t = 0; t < num_threads; t++)
{
rc = pthread_join(threads[t],NULL);
if(rc){
fprintf(stderr,"ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
//parallel region ends
}
pthread_exit(NULL);
}
//thread task
void* task(void* args)
{
pthread_exit(NULL);
}