11

Why have the calling thread walk into the async method until the inner 'await'?

Isn't it cleaner to just spawn a thread as soon as an async method is called. That way you know for sure that the async method returns immediately. You don't have to worry about not doing anything expensive at the early stages of the async method.

I tend to like to know whether a method is going to execute code on 'my' thread or not. Whether it's blocking or not. This model seems to open a whole spectrum of in-between possibilities.

The designers are much smarter than I am so I'm sure there is a good reason, I'd just like to get my head around it.

2
  • you don't place the async keyword for asynchronous methods but for methods that call asynchronous code :)
    – Tim Mahy
    Jan 13, 2012 at 17:01
  • Just to clarify: I love the syntactical sugar that means you don't need callbacks to do async ops. It's the way the threads flow that makes me uneasy for some reason. Jan 13, 2012 at 17:20

3 Answers 3

38

Isn't it cleaner to just spawn a thread as soon as an async method is called.

The whole point of "async" methods is to avoid spawning a new thread.

You are confusing asynchrony with concurrency. Asynchronous methods need not run on another thread to be asynchronous. The point of asynchronous methods is that they allow you to break up work into little pieces that need to run in a particular order, but not necessarily without doing other work on the same thread.

Think of a thread as a worker you can hire. Think of a async method as a to-do list with pauses between the items. If your to-do list says "go to the store, buy milk and eggs, go home, make an omelette", then the benefit of async is that when someone calls your cell phone between the "buy eggs" step and the "go home" step and says "can you stop by the pharmacy on your way home and pick up my prescription?" you can take the call and schedule the work before you make the omelette. With non-async methods, your phone keeps ringing until the omelette is done, and then you take the call. The UI blocks until you're done what you're doing.

Your concept is that in order to keep the UI thread responsive, the moment you get the to-do list you go hire some guy to run to the store for you, so that you're free to take the call about the pharmacy. That is expensive and unnecessary. Everything can stay on the same thread with async because the long-running task has built-in points where the UI gets to interrupt and schedule more work.

6
  • Great analogy! I guess this works fine as long as the time between pauses, when I'm unavailable to take a call, does not get too long. With a worker for hire, I am sure to NEVER miss a call. Being afraid of missing the call, when I have people I can hire for cheap (spawning threads) is the source of my uneasiness I guess, so the analogy works great even in explaining my own feelings. Accepted answer! Jan 13, 2012 at 17:41
  • 2
    @HarryMexican: If you consider spawning threads to be "cheap" then async is possibly not necessary for you in the first place. I consider spawning threads to be extremely expensive and therefore to be avoided at all costs. Jan 13, 2012 at 17:45
  • Fair enough. I don't do much UI programming so maybe that's why I've never realized how expensive they are... In any case, thread spawning being expensive does answer my question. Jan 13, 2012 at 17:49
  • 3
    For me avoiding the performance cost of additional threads isn't even the most compelling argument. It's avoiding the complexities of having my core code run on multiple threads. Jan 13, 2012 at 19:48
  • 1
    @LBushkin: Right; the work, or part of it, could happen on another thread. But the idea that an asynchronous method is inherently a method that will start up another thread is mistaken. Jan 13, 2012 at 23:03
3

I like to think of async..await to be syntactic sugar for continuation-passing style programming.

With that in mind it has nothing to do with threads.

2

I tend to like to know whether a method is going to execute code on 'my' thread or not.

I think that is a peculiar desire, not really a good argument for/against any feature.

The main point of async/await is that the code for starting an async op and handling the results can be kept into one method.

Without it you are forced to break code that logically belongs together into 2 parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.