I'm writing a multi-threaded c++ application using sockets on linux. I
have a primary thread which repeatedly calls select, listening for
activity on any of the sockets. It passes off the processing of the
active sockets to some worker threads and recalls select minus the
active sockets. Once a worker thread has finished with a socket, it
needs to return the socket back to the set of sockets the primary thread
is listening to.
My problem is the primary thread may be blocked on select when a worker
thread tries to return the socket it is processing, thus I need some way
of forcing the primary thread to return from the select call. The
solutions and my thoughts are as follows:
1) Add a timeout to the select call. Quite possibly the simplest
solution but AFAIC this is just a disguised form of busy wait which
should be avoided.
2) Raise EINTR signal. This has the side effect of causing every other
thread that might be blocked on a similar function (e.g. send or recv)
to return as well. It can be handled but is an extra degree of
complexity I'd rather not have to deal with.
3) Add the descriptor of one end of a pipe or socketpair to the
listening set. Then send some dummy information on the other end to
trigger a return from select. This is the best solution that I can come
up with that will target a particular thread and is relatively simple to
implement.
Comments, suggestions, advice would be very much appreciated. :)
Andrew Heath