Lots of info can be found, but I still unable to make a decision how to proceed.
Application creates a number of threads, some of them perform poll()
, some of them are in blocking read()
. I need to ensure termination of each process in specific sequence, this means it is not acceptable to just kill everything at once (unless it is emergency kill).
This one explains an elegant way to do the things, but is it the only and best way?
Right now, I set up the signal handler for INT, TERM and HUP (leaving USRx for emergency termination). I do not block signals. In the signal handler I set the flag and raise the event for main()
process waiting in poll()
. The process (main) gets out of the poll()
, but not because of the raised event, but because of erro 4 (interrupted system call), this, IMHO, means that signal gets delivered to the main thread, and having signal handler does not clear the signal condition. Next, I am not sure if signal, as soon as it is being delivered to the main, not being delivered anywhere else within the process’ thread infrastructure.
The related issue, why I actually raised this matter at all, is that it seems I have to explicitly deliver INT signal to blocking read()
in some threads, otherwise they do not notice the termination signal and keep waiting. If, because of incorrect signal handling implementation, threads start dying in random order, I risk to call pthread_kill()
on already terminated therads, which will have, per specification, undefined consequences.