[OT] poll() vs. AIO (was: [PATCH] ash: clear NONBLOCK flag from stdin when in foreground)

Rich Felker dalias at aerifal.cx
Thu Aug 18 23:09:52 UTC 2011


On Fri, Aug 19, 2011 at 12:36:32AM +0200, Denys Vlasenko wrote:
> > (very) pseudo-code of the good main application loop:
> > 
> >     while (read(tqfd, &event, sizeof event) == sizeof event)
> > 	switch (event) {
> > 		case keystroke: ...
> > 		case menu_item: ...
> > 		case mouse_click: ...
> > 		case window_refresh: ...
> > 		case timer_exp: ...
> > 		case read_done: ...
> > 		case write_done: ...
> > 		case connection_made: ...
> > 		case connection_died: ...
> > 		case child_exit: ...
> > 		default: ...
> >       }
> >     exit(1);
> 
> This is nice in theory, but as soon as one of "..." parts
> requires blocking, you need to split it into tow (or two dozen)
> subparts and include them separately into this gigantic switch.
> 
> This becomes unwieldy very quickly. BTW, main loops based on
> select/pool are no different: they have absolutely the same problem.

The problem goes away nicely as soon as you give up the idea that
threads are the work of the devil; you end up with several threads
doing something like the above, and when you have a case that needs to
do something that can't complete immediately, you pass the work off to
another thread.

Threads got a really bad name from the bloat of popular
implementations. One of my design goals in musl has been to make
threads so light and inexpensive (in both code size and runtime memory
and cpy time overhead) that you don't have to think twice about using
them.

Rich


More information about the busybox mailing list