[Libevent-users] Add support for WINDOWS Overlapped IO
Toby Douglass
toby.douglass at summerblue.net
Sun Dec 10 17:48:37 EST 2006
Steven Grimm wrote:
> Toby Douglass wrote:
>> Single-thread event-driven seems to me to basically mean state
>> machine. State machines are wonderful things for achieving simple,
>> bug-free code, but they have a cost; they are implicitly
>> single-threaded. This can mean you cannot use them in some
>> situations, because you will inherently block whenever you perform work.
> That's not strictly true; you can, for example, have multiple state
> machines running concurrently (the implementation strategy I used when I
> added thread support to memcached).
I had a situation where I had a large number of pipes being waited on,
in a state machine - so there was a state which was where I called
WaitForMultipleObjects() and sat there waiting for one of them to come
back to me. When one of them did come back to me, I'd go off into the
state machine and process its request. Of course, while doing that, I
couldn't respond to any of the other pipes. If the request was time
consuming, I was stuffed!
Now, as you say, I could have written a state machine to go and do the
work for that pipe...but that means running that in another thread.
This is what I did - and began running into the synchronization problems
that avoiding was the point of having a state machine in the first
place. (E.g. has that first request been serviced yet, will the
requests other people make in the meantime depend on the results of that
first request, etc).
> Or you can have a single thread
> responsible for I/O operations that hands work off to other threads that
> perform whatever blocking operations they need. Or you can combine the
> two and have a small set of I/O management threads and a larger set of
> worker threads.
I think the more threads you have, the more synchronization you'll need
to be doing between the state machines, and the more likely you are to
get it wrong.
That's the lovely thing about a state machine; it serialises operations.
That removes a whole class of mistakes.
More information about the Libevent-users
mailing list