I've got a multithreaded server (using POSIX threads), with one thread for each persistent connection. In one of the threads the other end of the connection closes, resulting in a SIGPIPE being delivered. Is there a (preferably portable) to determine which thread (and so which connection) this happened for so I can have my signal handler either do the thread/connection cleanup work itself or set a flag so that the main and worker thread respectively see that they need to do it later?
EDIT: I'm wondering if I could perhaps use &errno, store it in a global array and associate it with the server's identifier for the thread, and then search for &errno in the signal handler. Would the thread's specific errno be visible to the signal handler? Is my understanding of how the threadsafe errno works even in the ballpark?
I don't think so, no. A better solution for multithreaded servers is to suppress the SIGPIPE
signal (by calling signal(SIGPIPE, SIG_IGN)
as part of the program's startup routine) and then have the thread deal with the error value (-1/EPIPE) returned by send()
instead.
Signals and multithreading don't mix well.