3

This is something I don't understand. Suppose I

DoSomething();
await SomeAsyncMethod;
DoSomethingElse();

and my machine can only run 1 task at a time. Then SomeAsyncMethod has to block the program in some way -- maybe it doesn't immediately block DoSomethingElse() from running, it will have to interrupt the program at some point so that it can run SomeAsyncMethod. Right?

Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
  • "my machine can only run 1 task at a time" is true(ish) in the case of single core processors, but that "task" is the operating system, which is waking and sleeping (possibly) hundreds of different threads using pre-emptive multi-tasking. You program gets a slice of processor time, but then so does everything else. – spender Jan 13 '16 at 00:49
  • You're confusing the concepts of asynchronicity and parallel processing. Async-await isn't parallel, it's asynchronous. – Enigmativity Jan 13 '16 at 04:22
  • 5
    You put some toast in the toaster. While the toast is toasting, you read the junk mail that arrived yesterday. Does the toaster require you to hire another cook to do nothing but watch the toaster? No. Does the toaster require you to stop reading your mail when the toast pops? No. And yet you are only doing one thing at a time -- you are either putting toast in, or reading the mail. Now is it clear how it is possible that an asynchronous method can do work -- toasting toast -- without blocking the CPU -- you reading your paper -- or hiring another CPU? – Eric Lippert Jan 13 '16 at 04:40
  • Read [these articles](https://docs.com/paulo-morgado/8860/async-await-general). In particular, read [There Is No Thread](http://blog.stephencleary.com/2013/11/there-is-no-thread.html). – Paulo Morgado Jan 13 '16 at 07:31
  • @EricLippert - I think you should expound on that as an answer here - maybe mentioning how this elucidates difference b/w asynchronous vs parallel? – Don Cheadle Nov 21 '16 at 02:30

1 Answers1

5

Then SomeAsyncMethod has to block the program in some way

No, it doesn't. It's asynchronous. The definition of what it means to be asynchronous means that it doesn't block; the caller will continue on after the method returns, without it having done its work, and the work will be completed at a later point in time.

it will have to interrupt the program at some point so that it can run SomeAsyncMethod. Right?

No. It's asynchronous. It may not even be work that needs to be performed by a CPU at all.

Let's go with an analogy here. You order a package. Are you prohibited from ever doing anything until that package arrives? Are you forced to sit in front of your house explicitly waiting for that package, incapable of doing anything else until it arrives? Or can you go off, do other work, and then at some point in time, when the package arrives on your steps (not necessarily immediately, you can go pick it up at any arbitrary point after it has been delivered) you can open it up and do whatever it is you needed to do with what you ordered.

This is an inherently asynchronous operation. You start the asynchronous operation (order something) then you can do whatever you want until the operation you started is completed, without it occupying your time at all, and then at some point in time, after the operation has finished, you can use the results of that operation.

One type of asynchronous operation that you can perform when programming is asking another thread to do some CPU bound work, and that work may (or may not) be performed on a second processor, but there is all sorts of other types of asynchronous operations that need no other processors, such as IO (performing a network request, for example) which is virtually always inherently asynchronous, and lines up pretty nicely with the "order a package" analogy, there's performing an action after some trigger (an event, etc.), you can have an asynchronous operation that is itself composed of one or more other asynchronous operations, etc.

See this post for another example explained in a bit more depth.

Community
  • 1
  • 1
Servy
  • 202,030
  • 26
  • 332
  • 449