If async-await doesn't create any additional threads, then how does it make applications responsive?
See the question and my original answer on StackOverflowHere is how I view all this, it may not be super technically accurate but it helps me, at least :).
There are basically two types of processing (computation) that happen on a machine:
- processing that happen on the CPU
- processing that happen on other processors (GPU, network card, etc.), let's call them IO.
So, when we write a piece of source code, after compilation, depending on the object we use (and this is very important), processing will be CPU bound, or IO bound, and in fact, it can be bound to a combination of both.
Some examples:
- if I use the Write method of the
FileStream
object (which is a Stream), processing will be say, 1% CPU bound, and 99% IO bound. - if I use the Write method of the
NetworkStream
object (which is a Stream), processing will be say, 1% CPU bound, and 99% IO bound. - if I use the Write method of the
Memorystream
object (which is a Stream), processing will be 100% CPU bound.
So, as you see, from an object-oriented programmer point-of-view, although I'm always accessing a Stream
object, what happens beneath may depend heavily on the ultimate type of the object.
Now, to optimize things, it's sometimes useful to be able to run code in parallel (note I don't use the word asynchronous) if it's possible and/or necessary.
Some examples:
- In a desktop app, I want to print a document, but I don't want to wait for it.
- My web server servers many clients at the same time, each one getting his pages in parallel (not serialized).
Before async / await, we essentially had two solutions to this:
- Threads. It was relatively easy to use, with Thread and ThreadPool classes. Threads are CPU bound only.
- The "old" Begin/End/AsyncCallback asynchronous programming model. It's just a model, it doesn't tell you if you'll be CPU or IO bound. If you take a look at the Socket or FileStream classes, it's IO bound, which is cool, but we rarely use it.
The async / await is only a common programming model, based on the Task concept. It's a bit easier to use than threads or thread pools for CPU bound tasks, and much easier to use than the old Begin/End model. Undercovers, however, it's "just" a super sophisticated feature-full wrapper on both.
So, the real win is mostly on IO Bound tasks, task that don't use the CPU, but async/await is still only a programming model, it doesn't help you to determine how/where processing will happen in the end.
It means it's not because a class has a method "DoSomethingAsync" returning a Task object that you can presume it will be CPU bound (which means it maybe quite useless, especially if it doesn't have a cancellation token parameter), or IO Bound (which means it's probably a must), or a combination of both (since the model is quite viral, bonding and potential benefits can be, in the end, super mixed and not so obvious).
So, coming back to my examples, doing my Write operations using async/await on MemoryStream will stay CPU bound (I will probably not benefit from it), although I will surely benefit from it with files and network streams.