4 ms·
This is why I generally prefer event-driven programming over multithread programming. This is the sort of programming you do, for example, when you are implemen
by cduan 17y ago
This is why I generally prefer event-driven programming over multithread programming. This is the sort of programming you do, for example, when you are implementing a GUI: you write short event handler functions that respond to events (such as mouse clicks or the completion of network or disk reads), and then a master loop receives events and dispatches the handler functions.
You get the same benefits as concurrency (maximum processor usage, at least when you only have one processor), but you avoid the pitfalls of shared memory, race conditions, and unexpected state changes (because only one event handler runs at a time). It does take a bit of getting used to (it seems so easy to make function calls like time() or mkdir() that are actually blocking and thus technically should be separated into a new callback function). But I find that the benefits of processor efficiency plus the comfort of avoiding concurrency problems are worthwhile.
http://en.wikipedia.org/wiki/Event-driven_programming http://en.wikipedia.org/wiki/Event-driven_programming
- ori_b 17y agoThe point that I was trying to make is that a well designed concurrent program is often easier to reason about than an event driven program. Even if it didn't have benefits scaling to multiple processors and hiding latency, concurrent programming would have advantages in clearly structuring programs. A great talk about the advantages of concurrent programming in producing clear and understandable program structure can be seen in Rob Pike's newsqueak tech talk (http://video.google.com/videoplay?docid=810232012617965344 http://video.google.com/videoplay?docid=810232012617965344)