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":
- Create a pipe with
pipe()
;
- The target thread, instead of blocking in
recvfrom()
, blocks in poll()
or select()
. The file descriptors to monitor include the datagram socket and the read end of the pipe.
- To wake up the target thread, the other thread writes a single byte to the write end of the pipe.
(The recvfrom()
should also be changed to use the MSG_DONTWAIT
flag, since we never want to block here, and always block in the poll()
/ select()
instead).