2

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?

casperOne
  • 72,334
  • 18
  • 180
  • 242
thejh
  • 43,352
  • 16
  • 93
  • 105

2 Answers2

4

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

James McLaughlin
  • 18,774
  • 3
  • 47
  • 56
1

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.

Community
  • 1
  • 1
Kenster
  • 20,846
  • 21
  • 74
  • 99