Does anyone know of a way to determine whether a thread is currently blocking? Basically, I want to check whether a certain thread is blocking (in this case on a AF_UNIX datagram socket receive call) and send it a signal to interrupt if it is.
I'm working on Linux using Boost.Thread meaning underneath I'm using pthreads. My system has NPTL.
I think the answer is "no", but want to see if I'm missing something.
This is not easily possible (it is possible using the functionality that is intended for debuggers, but it is neither simple, portable or safe).
You do not really want to do this anyway, because such a use would have an inherent race condition. You may check if the thread is blocking just before it is about to block (in which case you would miss waking it), or it may stop blocking just after you find that it is blocking.
The usual method to solve your problem is the "self-pipe trick":
pipe()
;recvfrom()
, blocks inpoll()
orselect()
. The file descriptors to monitor include the datagram socket and the read end of the pipe.(The
recvfrom()
should also be changed to use theMSG_DONTWAIT
flag, since we never want to block here, and always block in thepoll()
/select()
instead).