c++linuxmultithreadingpthreads

SIGINT while doing std::thread::join()


I have a main(), which spawns a thread, and then joins with it. I want to be able to CTRL-C the program, so I would install SIGINT handler in main (the spawned thread will ignore this signal). When I am in sig-handler I will cancel the spawned thread with cancel(), but what happens with the current 'join()', which was active during the signal invocation?

My guess is that I will get EAGAIN or EINTR, and I would have to call join() in a loop. Am I right?

The question is: Is this legal with multithreading? I don't mind just setting a flag within the SIGINT handler, but what happens with the join() call?


Solution

  • Signals and threads? Here be dragons! You have to fully-specify the masks, or else any thread may receive the signal.

    The signal handler should generally not assume it is running in the "main" thread. Rather, it should post a message and return, analagously to thread interruption. The main thread can pick this up later in an event loop or whatever and then join.