How to interrupt a call to select

How to interrupt a call to select

Post by Andrew Heat » Tue, 15 Oct 2002 17:58:27



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

 
 
 

How to interrupt a call to select

Post by compbo » Tue, 15 Oct 2002 23:16:28


Quote:> 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.

This #3 is the best method I have found and it has worked well for me over
the years.  I use a pipe.

 
 
 

How to interrupt a call to select

Post by Sean P. Bur » Wed, 16 Oct 2002 02:32:14



> > 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.

> This #3 is the best method I have found and it has worked well for me over
> the years.  I use a pipe.

And, rather than writing dummy information to the pipe,
just write the fd that is to be added back into the
select set to the pipe.

-SEan

 
 
 

How to interrupt a call to select

Post by Andrew Heat » Wed, 16 Oct 2002 16:23:05




>>>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.

>>This #3 is the best method I have found and it has worked well for me over
>>the years.  I use a pipe.

> And, rather than writing dummy information to the pipe,
> just write the fd that is to be added back into the
> select set to the pipe.

> -SEan

Ahh, thanks very much guys. Just what I needed to know. :)

Andrew