Reputation: 51
I am in a gaming scenario. Both the server and clients exchange messages through non-blocking UDP in a high rate.
(Maybe odd here...) The legacy code also uses select()
with timeout value set to 0
, which means select()
does not block. select()
is within a forever while loop. Upon select()
returns a number greater than 0
, following code goes receive message through recvfrom()
. If it returns 0
, following code does not try receiving.
From print-out info, I saw select()
sometimes returns 1
(greater than 0
). I am confused that since timeout is set to 0
, how does select()
have time to check if a message is ready to read from any of readfds
? Thanks.
Upvotes: 2
Views: 1287
Reputation: 71525
According to the spec:
Upon successful completion, the pselect() or select() function shall modify the objects pointed to by the readfds, writefds, and errorfds arguments to indicate which file descriptors are ready for reading, ready for writing, or have an error condition pending, respectively, and shall return the total number of ready descriptors in all the output sets. For each file descriptor less than nfds, the corresponding bit shall be set upon successful completion if it was set on input and the associated condition is true for that file descriptor.
If none of the selected descriptors are ready for the requested operation, the pselect() or select() function shall block until at least one of the requested operations becomes ready, until the timeout occurs, or until interrupted by a signal. The timeout parameter controls how long the pselect() or select() function shall take before timing out. If the timeout parameter is not a null pointer, it specifies a maximum interval to wait for the selection to complete. If the specified time interval expires without any requested operation becoming ready, the function shall return. If the timeout parameter is a null pointer, then the call to pselect() or select() shall block indefinitely until at least one descriptor meets the specified criteria. To effect a poll, the timeout parameter should not be a null pointer, and should point to a zero-valued timespec structure.
In short, checking the descriptors happens before checking the timeout. If the socket already has data ready when select()
is called, the timeout is ignored and select()
exits immediately.
Upvotes: 1