http://stackoverflow.com/questions/3161916/why-is-the-asynchronous-mode-is-better-than-the-synchronous-one-when-handling-cl
Yes, asynchronous requests can often be handled without the expense of a thread. The operating system has special support for them, features like overlapped I/O and completion ports. What they essentially do is leverage the expense of a kernel thread, one that's needed anyway because a driver needs to be able to handle multiple requests from multiple user mode programs. The .NET framework readily takes advantage of that in its BeginXxx() methods.
Using threadpool threads is cheap too, but you are subject to the behavior of the threadpool scheduler. Which doesn't much like starting more TP threads then there are cores. TP threads should never be used for code that can stay blocked for a while, pretty typical for CS tasks like making a connection.
Error handling is very difficult in asynchronous code. You typically have very little context when the EndXxxx() method raises an exception. It happens on a callback thread, very far away from the main logic. Okay when you can shrug "didn't happen, lets log it", total bedlam and redrum when the program's state depends on it. Always choose synchronous mode in the latter case.
本文探讨了异步模式相较于同步模式在处理客户端请求时的优势。通过利用内核线程和特殊的系统支持如重叠I/O及完成端口等特性,异步请求能够有效地避免线程开销。同时,文章也指出了异步错误处理的复杂性和局限性,并建议在状态依赖于错误处理的情况下使用同步模式。
1765

被折叠的 条评论
为什么被折叠?



