[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] O_NONBLOCK and select/epoll/libevent



On Mon, 2009-10-26 at 21:32 -0700, Evan Klitzke wrote:
> that was susceptible to this race, and I see the O_NONBLOCK usage in
> single-threaded programs. So are there any other legitimate uses for
> this pattern?

Yes.  It's often a good idea, for efficiency reasons, to read
continuously from a socket until you get an EWOULDBLOCK error.  You're
saving yourself the overhead of going back to the event loop just to
have it call epoll, have it return and have you read more from the
socket.

The same logic applies to writing to a socket.

Of course, then you end up with potential scheduling issues.  If, for
example, your socket is connected to some program that's dumping data as
fast as possibly it can cause your program to only read from it and
thereby starve all the other sockets you might be connected to.  So it's
good to have an upper limit on how much data you will read from a socket
in one go, even if you are using the 'read until EWOULDBLOCK' pattern.
This is analogous to a time slice in a preemptive scheduler.

Have fun (if at all possible),
-- 
A word is nothing more or less than the series of historical
connotations given to it. That's HOW we derive meaning, and to claim
that there is an arbitrary meaning of words above and beyond the way
people use them is a blatant misunderstanding of the nature of language.
-- Anonymous blogger
-- Eric Hopper (hopper@xxxxxxxxxxxxxxx http://www.omnifarious.org/~hopper)--

Attachment: signature.asc
Description: This is a digitally signed message part