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
  • 73,706
  • 19
  • 184
  • 253
thejh
  • 44,854
  • 16
  • 96
  • 107

2 Answers2

4

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

James M
  • 18,506
  • 3
  • 48
  • 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
  • 23,465
  • 21
  • 80
  • 106