Asynchronous event loop design and issues.
Posted
by Artyom
on Stack Overflow
See other posts from Stack Overflow
or by Artyom
Published on 2010-05-06T09:18:04Z
Indexed on
2010/05/06
18:58 UTC
Read the original article
Hit count: 324
Hello,
I'm designing event loop for asynchronous socket IO using epoll/devpoll/kqueue/poll/select (including windows-select).
I have two options of performing, IO operation:
Non-blocking mode, poll on EAGAIN
- Set socket to non-blocking mode.
- Read/Write to socket.
- If operation succeeds, post completion notification to event loop.
- If I get EAGAIN, add socket to "select list" and poll socket.
Polling mode: poll and then execute
- Add socket to select list and poll it.
- Wait for notification that it is readable writable
- read/write
- Post completion notification to event loop of sucseeds
To me it looks like first would require less system calls when using in normal mode, especially for writing to socket (buffers are quite big). Also it looks like that it would be possible to reduce the overhead over number of "select" executions, especially it is nice when you do not have something that scales well as epoll/devpoll/kqueue.
Questions:
- Are there any advantages of the second approach?
- Are there any portability issues with non-blocking operations on sockets/file descriptors over numerous operating systems: Linux, FreeBSD, Solaris, MacOSX, Windows.
Notes: Please do not suggest using existing event-loop/socket-api implementations
© Stack Overflow or respective owner