Mark’s Blog

December 9, 2008

The easy way out.. or is it?

Filed under: Programming — mheily @ 8:42 pm

Some problems are so hard, you are tempted to take the easy way out. Multiplexing I/O using poll(2) and non-blocking read(2) calls has a number of tricky corner cases. After writing and rewriting it several times, I was tempted by the dark side.. threads.

I thought that giving each session a dedicated thread and using fgets(3) to read lines would be easy. However, it is also very wasteful. The stack space required for 1000 threads (at 2MB per thread) would be 2GB. Plus, fgets(3) doesn’t tell you how many bytes were read, so you would have to call strlen(3) after each line of input was read.

The good news is, I wrote a pretty good thread pool implementation and will use thread pools for certain phases of the session that require blocking I/O, such as the fsync(3) and gethostbyname(3) calls. It’s also not too hard to revert the code changes and go back to an event-driven model.

Powered by WordPress