I have a program that is supposed to execute a number of functions, each one in its own thread. Once one of them is finished (or a timeout is reached), all threads are terminated. As I don't have access to all of the individual functions (they are rather complicated external libraries) this is achieved by signalling the threads and jumping into some Cleanup section, from where the threads then exit. For the most part, this works as intended.
However, sometimes one of the threads can't be joined, i.e. the main thread sends a signal, the signal handler is invoked, execution jumps into the cleanup section, the section is executed, but pthread_join() then blocks in the main thread.
void * worker_thread(void *args){
//Install sig handler
//If signal received, goto CLEANUP;
//Launch function
CLEANUP:
//Some cleanup
puts("leaving thread"); //This is executed
pthread_exit(NULL);
}
int main(){
//initialise data etc
pthread_t worker;
pthread_create(&worker, Null, worker_thread, args);
// Some stuff
if(timeout || done){
pthread_kill(&worker);
pthread_join(&worker, NULL); //execution blocks here
}
return(0);
}
I have played around with a lot of code around this, but it really seems to come down to this pthread_join(). In case it's useful, strace gives:
[pid 19153] futex(0x7f1178000020, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 19150] futex(0x7f119119c9d0, FUTEX_WAIT, 19153, NULLstrace: [ Process PID=19154 runs in 64 bit mode. ]
which, as far as i understand it, supports my hypothesis that the join / exit pair is blocking in some way.
In any case, if anyone has encountered something like this before or knows what might be causing it, it would be massively helpful to have some input on that.
edit:
I'm aware that signals are not the ideal way to do anything. However, I've tried a lot of non-signal solutions, and none of them work. I need to be able to terminate execution of some function that may take years to run, and for which I have no control over its construction. As far as I can tell, only signals will do this. (If you are wondering, yes, this leaks all of the memory. It's a price I'm currently willing to pay)