I am reading conflicting opinions here. Am I right that although an asynchronous method call also gets handled by a thread pool invisible to the user, this is likely to save resources because the underlying operating system is able to suspend these threads whilst IO calls are pending ?
-
1Lots of threads require constant context switching.diolemo– diolemo2012-03-02 04:27:08 +00:00Commented Mar 2, 2012 at 4:27
-
Please don't prefix your titles with ".net" and such. That's what the tags are for.John Saunders– John Saunders2012-03-02 04:42:07 +00:00Commented Mar 2, 2012 at 4:42
-
Lots of ready threads require constant context switching.Martin James– Martin James2012-03-02 08:34:50 +00:00Commented Mar 2, 2012 at 8:34
3 Answers
You are exactly right. The IO threads are not only suspended, they are retired when they become unneeded. But async IO is not a general solution for all problems and here is why:
- Async algorithms are hard to code. The async code is more complicated and error-prone than synchronous variant
- IO completion callbacks work in special IO threads and programmer has to keep those threads as free as much it is possible; otherwise the system will slow down significantly. So if you go to async IO be ready to implement Producer-Consumer pattern for actual data handling
- If you have a demand in less than 150 parallel connections and the application runs on a PC machine then synchronous implementation will be a low-hanging fruit delivering ease of programming and satisfactory performance at the same time
1 Comment
'an asynchronous method call also gets handled by a thread pool invisible to the user' - yes, something must do the I/O!
'this is likely to save resources because the underlying operating system is able to suspend these threads whilst IO calls are pending' - the OS is able to suspend user threads while I/O is pending as well.
In high-performance apps, asynchronous wins because more work is pushed into the kernel where better thread management is possible. This does reduce context switches and also avoids a lot of data copying.