thejh
thejh

Reputation: 45568

How can I tell a thread that is currently or will soon be blocking in poll() to do something?

I have a thread A that calls poll() in a loop. This thread is responsible for IO on incoming and outgoing connections. However, when thread B has opened an outgoing connection, it has to give it to A somehow. Do you think it'd be a good idea to have a pipe between threads A (reading side) and B (writing side) that B writes to after opening the socket?

Upvotes: 2

Views: 99

Answers (2)

Kenster
Kenster

Reputation: 25390

Rather than an actual pipe, you could look at using a socket pair. You could create a unix-domain socket pair and send messages to the blocked thread using send() or sendmsg(). This approach might be more convenient for you. Unix-domain sockets also support passing file descriptors between processes, although that's overkill for your application.

The other approach is to interrupt the call to poll() with a signal. See this question.

Upvotes: 1

James M
James M

Reputation: 16718

That sounds reasonable. poll should be happy watching a pipe alongside your socket(s).

Upvotes: 4

Related Questions