Make select () track without writing to desc?

I have this thread in my application that controls a set of client sockets. I use select() to block until the client makes a request so that I can handle it efficiently without multiplying the threads.

Now the problem is that when I add a new client to the list of clients, I have to wait for the select() timeout (set to 10 seconds) to actually add a new socket to the listening sockets.

So, I would like to crack select() before the timeout so that I can immediately listen to the client.

I already have a solution: create a dummy socket, which I always include in the list of listening sockets, and in which I write to make select() crack, but I hope that there will be a better solution.

Edit : I do not have access to eventfd() , because the GLibc that I use is too old (and I do not mean to update it). Therefore, I may have to use fifo or a socket.

Do you know anyone?

Thanks!

+4
source share
4 answers

The usual way to wake a select loop is to add the end of the pipe() pair reading to the selected viewset. When you need to wake up the selection loop, write some dummy data at the end of the file descriptor entry.

Note that on linux, you can also consider using eventfd() instead of pipe() - it can be somewhat more efficient (albeit less portable).

You can also process the listening socket in the selection loop, and not transfer it to another thread - this will quietly start the selection loop when a new client appears.

+7
source

You can use the same select() call to wait for an incoming connection by including it in the FD set; that way, when it indicates that the connection is waiting, you can accept the connection without blocking and add a new file descriptor to the set.

+3
source

You can raise a signal to force EINTR to select select (), but signal processing in multi-threaded programs is black magic, and socketpair () is simpler and more reliable.

0
source

You can use WSAEventSelect to bind a Windows socket to HEVENT and wait, then bind to a HEVENT socket with other descriptors using WaitForMultipleObjects .

0
source

Source: https://habr.com/ru/post/1338718/


All Articles