My program creates some pthreads in a for loop that do some work, then waits for them to finish in another for loop using pthread_join and then it is supposed to show some output. The problem is that the program doesn't get passed the pthread_join loop, I've stepped through the code with a debugger and it seems that at the second iterration of the loop the program just terminates with exit code 0.
for(i = 1; i <= n_cust; i++){
ids[i]= (int) i;
int rc = pthread_create(&thread_ids[i], NULL, &thread_transaction,
(void *) (&ids[i]));
if (rc != 0) { //no error, all threads are created and print a result
printf("ERROR: return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(j=0; j < n_cust; j++) {
if(thread_ids[j] != null){
int rc = pthread_join(thread_ids[j], NULL);
if (rc != 0) {//program terminates before being able to check rc
printf("ERROR: return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
}
printf("SEAT ARRANGEMENT: \n");
for(y=0; y<Nseat;y++){
printf("Seat %d / Costumer %d, ", y, seats_array[y]);
}
When i printed the value that pthread_join returns, at first I got 3 that means that a thread is null, but later I tried checking if the current thread is null before joining it and then the program just stops with exit code 0, before being able to print the rc variable. The threads seems to be doing their work correctly, since all of them print the excpected outputs. Also I don't know if this is helpful but I am building the program in windows with the windows pthread library for convenience, although I am supposed to do it in linux, or at least make sure that it works in a virtual machine. If you find it neccessary i can provide all the code of the function that gets passed in the thread. Thanks in advance.