2

AFAIK - ( and I read a lot about it), asynchronous methods (not asynchronous delegates !) exists to solve the "thread is blocked" problem when dealing with I/O operations like : reading a file or downloading a file :

Richter shows it quite clearly here :

enter image description here

  • Task<T> is not related to the i/o blocking issue. it is simply just like open a thread ( plus extra efficiency + functionality ) - but it still causes a thread to consume cpu quanta etc.

And here is my question :

I've read (msdn) that :

An async method provides a convenient way to do potentially long-running work without blocking the caller's thread. The caller of an async method can resume its work without waiting for the async method to finish.

  • Is it just like creating a Task<t> with ContinueWith ?

  • Doesn't the terminology is confusing ? asynchronous methods are for i/o operations (where there are zero threads waiting while i/o operation is made and no thread is dealing with it). But to call a code (which use async) as: asynchronous methods is a bit confusing. don't you think ? because I assume there is another thread which is executing...(which is my first question actually).

Where is the confusion from ?

Because Albahari tend to emphasize what Asynchronous methods are for :

enter image description here

p.s. I've read a few question here at SO regarding this topic , but found none which talks about the misclassification that asynchronous methods are here to deal with io operations

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    maybe you already seen this video,if not it might help a bit http://channel9.msdn.com/Events/Build/BUILD2011/TOOL-829T – terrybozzio Jun 15 '13 at 21:44

2 Answers2

4

Task<T> is not related to the I/O blocking issue. It is simply just like open a thread (plus extra efficiency and functionality) - but it still causes a thread to consume CPU quanta etc.

Not necessarily. Basically there are two kinds of Tasks: one executes a synchronous piece of code and completes when that code finishes executing. This kind of Task blocks a Thread the whole time since it starts executing until it's completed (successfully or not).

But there is another kind of Task: one that completes when something happens. This kind of Task is what .Net 4.5 and C# 5.0 use heavily and it doesn't block a Thread (at least not directly). You can create such Task yourself by using TaskCompletionSource<T>.

(Another point is that a blocked thread doesn't consume any CPU, but that's not really relevant here.)

Is it just like creating a Task<t> with ContinueWith?

Yes, await t is quite similar to t.ContinueWith(rest of the method).

Doesn't the terminology is confusing? Asynchronous methods are for I/O operations (where there are zero threads waiting while I/O operation is made and no thread is dealing with it). But to call a code (which use async) as: asynchronous methods is a bit confusing. Don't you think? Because I assume there is another thread which is executing.

I don't see the confusion. Classic asynchronous method (like BeginRead(); this is called “Asynchronous Programming Model” or APM) is a way of starting an operation and being notified when it completes (though a callback). Modern async method (like ReadAsync(); this is called “Task-based Asynchronous Pattern” or TAP) is also a way of starting an operation and being notified when it completes (using await).

In both cases, there can be some code that executes before the method returns (the code before the first await in the TAP case).

In both cases, the usual way of being notified about the result doesn't block any threads (callback for APM, await for TAP).

In both cases, you can use blocking wait if you want (immediately calling the EndXxx() method for APM, Wait() for TAP).

Both cases can be used to execute synchronous code on a background thread (BeginInvoke() on a delegate for APM, Task.Factory.StartNew() for TAP).

Again, I don't see the confusion, the two models seem very similar to me.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Hi. What do you mean _"two kinds of Task"_ ? there is just `Task` which is executed at another thread. ( and each thread get quanta). I dont understnad what 2 tasks you're talking about. – Royi Namir Jun 15 '13 at 16:01
  • @RoyiNamir There is just one type (`Task`), but it can actually represent two different kinds of `Task`s. And the other kind of `Task`s doesn't use a `Thread`. Have you looked at `TaskCompletionSource`? – svick Jun 15 '13 at 16:04
  • [yeah of course I know it](http://stackoverflow.com/questions/15316613/real-life-scenarios-for-using-taskcompletionsourcet) But itss just a plugin to an arbitary function as a **shell of a task**. ( so i can use it as a task) but its final result is a task. (another usage is http://i.stack.imgur.com/hyAlG.jpg). and of course thanks for reply ( as always) – Royi Namir Jun 15 '13 at 16:06
  • @RoyiNamir The point is, this kind of `Task` doesn't directly use a `Thread`. So it cam complete when a timer ticks or when an APM method callback is called or something like that. Which means it really is asynchronous. – svick Jun 15 '13 at 16:11
  • Ok . Albahari put a lot of effort to emphesize that asynchrouns methods are used for io only operations. please read the **second paragraph** http://books.google.co.il/books?id=VENrFSQFco8C&pg=PA927&lpg=PA927&dq=%22The+problem+just+described+might+be+insoluble+if+every+thread+needed+to+be+busy%22&source=bl&ots=3uYZli808V&sig=eyDe6pdWBUnyacvqRzeTHYEPFD8&hl=en&sa=X&ei=FpO8UYnTDITvsgbk44CAAQ&redir_esc=y#v=onepage&q=%22The%20problem%20just%20described%20might%20be%20insoluble%20if%20every%20thread%20needed%20to%20be%20busy%22&f=false – Royi Namir Jun 15 '13 at 16:14
  • @RoyiNamir I still don't understand the confusion. TAP can also be used to execute many concurrent I/O requests on just a few threads. – svick Jun 15 '13 at 16:23
  • can you provide an example for _TAP can also be used to execute many concurrent I/O requests on just a few threads_ ? – Royi Namir Jun 15 '13 at 16:36
  • @RoyiNamir I don't know what you're expecting from me. If you for example have an ASP.NET application that uses `async` for some I/O and have lots of requests at the same time, that's exactly what will happen. Another example is when you start lots of asynchronous downloads in a loop and then use `await Task.WhenAll(downloadTasks)`. – svick Jun 15 '13 at 17:10
3

Async methods are not just for IO - they can be for anything - and yes, it's just another way of saying that the work of the method is executed in a separate thread. Any method where the calling thread offloads the work to a separate thread can be correctly called an "async" method. It is the same as Task<T> and ContinueWith -- ContinueWith is just another way of talking about a callback, really.

The word "asynchronous" simply means "not at the same time" - it can refer to any actions that happen independently of each other.

J...
  • 30,968
  • 6
  • 66
  • 143