I have this thread in my application that monitors a set of client sockets. I use select()
to block until a client makes a request, so that I can handle it efficiently without multiplying threads.
Now, problem is, when I add a new client to the list of clients, I have to wait for the timeout of select()
(set to 10 seconds) to actually add the new socket to the listened sockets.
So I'd like to make select()
crack before the timeout so that a client can be listened to immediately.
I already have a solution to that: create a dummy socketpair that I always include in my listened sockets list, and in which I write to make select()
crack, but I'm hoping there's a better solution out there.
Edit:
I have no access to eventfd()
because the GLibc I use is too old (and I have no mean to update it). So I might have to use a fifo or a socket.
Do you know any?
Thanks!
The usual way of waking up a select loop is to add the read end of a pipe()
fd pair to the select's watching set. When you need to wake up the select loop, write some dummy data to the write end of the file descriptor.
Note that on linux you might also want to consider using an eventfd()
instead of a pipe()
- it may be somewhat more efficient (although less portable).
You could also handle the listen socket in the select loop instead of handing it off to another thread - this would wake up the select loop implicitly when a new client comes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With