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
.
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);
}
Verified your code on an Ubuntu machine i7-4600U CPU @ 2.10GHz I don't see any issue with your code, just to verify i compiled and run your code on my local machine it seems to work fine.
i got the output as expected:
i:999,j:999
so I am assuming the issue you you are experiancing has something to do with your environment. what CPU OS and GCC are you using?