Jools in UK
Jools in UK

Reputation: 13

Does using Asynchronous method calls improve performance over .net threads

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 ?

Upvotes: 1

Views: 315

Answers (3)

Martin James
Martin James

Reputation: 24847

'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.

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

Ideally, if possible, you would use .net 4.0 System.Threading.Tasks.Parallel class. It will take advantage of multi-core processors.

And it's simple.

Upvotes: 0

ogggre
ogggre

Reputation: 2266

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

Upvotes: 1

Related Questions