libev: multiple writes in write callback?

Zsbán Ambrus ambrus at math.bme.hu
Mon Jul 4 08:51:44 CEST 2011


On Mon, Jul 4, 2011 at 4:49 AM, Aaron Boxer <boxerab at gmail.com> wrote:
> Is it acceptable to make multiple socket writes in the write callback?
> or multiple reads in the read callback?

If the file descriptor is set to non-blocking, then it is fine to do
multiple reads: if the kernel's read buffer becomes empty (or the
write buffer full), you will get an EAGAIN error.

If the descriptor is not set to non-blocking, then in general you
should not use multiple reads or writes (or accepts), for then the
first read could empty the kernel's buffer of incoming data, and then
the next read can block your process.  Of course, even a single read
or write might not be completed because there might not be as much
data (or buffer space) available as you asked for, but that's no
problem: the read does not normally block in that case, but instead
executes partially and returns the number of bytes read.  If you
wanted to do multiple reads, then either you have to wait for the next
readable event, or test whether the descriptor is still readable, of
which the simplest method is usually just using non-blocking
descriptors for everything.

Ambrus



More information about the libev mailing list