0

I understand async/await doesn't use real parallelism, and it can be used to better take advantage of the resources of a thread through concurrency. Also, I know I can create concurrency in a method by using "await" like this:

static async Task Main(string[] args)
{
    var task1= DoTask1Async(2);
    DoSomething();
    await task1;
}

On the other hand, the code below doesn't generate concurrency, it will be executed in a similar way to the conventional one (not using async/await). But is this technique useful for the system as a whole (the thread could be better used by other tasks), or is this code just useless?

static async Task Main(string[] args)
{
     await DoTask1Async(2);
     DoSomething();
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Marco
  • 13
  • 2
  • What do you mean by *"create concurrency"*? The difference between the first and second snippet is only in whether you wait for the task to finish before calling `DoSomething` or not – UnholySheep Jan 18 '20 at 20:47
  • 2
    Yes. It still frees up the thread. So that it can return to e.g. [keeping the UI responsive](https://stackoverflow.com/a/37419845/11683). – GSerg Jan 18 '20 at 20:50
  • @UnholySheep No, the difference is that in the first case `DoTask1Async` and `DoSomething` are "in flight" at the same time, in the second case `DoSomething` starts only after `DoTask1Async` completes. – GSerg Jan 18 '20 at 20:51
  • `var task1 = await DoTask1Async(2);` Something isn't right with this line. What is the actual type of the variable `task1`? – Theodor Zoulias Jan 18 '20 at 21:37
  • @GSerg aside from UI (let's say this code is running in a console app), it can still be useful? Will the thread be free use some idle time to perform something else? – Marco Jan 18 '20 at 22:45
  • @TheodorZoulias This code is just an example. You can assume task1 is a Task type, and the method DoTask1Async returns nothing (aside from the task itself). – Marco Jan 18 '20 at 22:47
  • 1
    @UnholySheep telling the code to await DoTask1Async() after DoSomething() is saying they both can run "at the same time", but not in real parallelism (they will still use the same thread), so there are concurrency in the thread to resolve the two tasks (which can be good if there is some idle time to be managed). – Marco Jan 18 '20 at 22:53
  • When you await a `Task` you get a `T` as a result. In your example the awaited result is `Task`, so the `DoTask1Async` should have a result of type `Task`. Which is strange, and makes your question even more puzzling. – Theodor Zoulias Jan 19 '20 at 00:12
  • 1
    @TheodorZoulias ok ok, now I see what was wrong in my second example. There is no return in awaiting the DoTask1Async. Now it's fixed. Hope it's less confusing now. – Marco Jan 19 '20 at 00:41
  • `await` frees up the thread to do other things in the meantime, so it's a good thing in general. (Though using it from `Main` is not a good idea.) – Raymond Chen Jan 19 '20 at 01:04
  • Wait what...? People can come and edit your posts here? That's way weird. Even weirder was the reason for the edit. Some guy complained about the term "I was wondering" like it could make the question unreadable, and demanded that I edited the post with a "real question". Note that it was so impossible to read that someone else came and "fixed" without asking me anything. I'm now wondering if this comment will be deleted, but I guess it's impossible for anyone to understand that sentence. – Marco Jan 19 '20 at 01:34
  • Any user with more than 2,000 reputation points can single-handedly edit any question or answer: [Privileges](https://stackoverflow.com/help/privileges). – Theodor Zoulias Jan 19 '20 at 02:09
  • So if I understand correctly, your question is whether employing concurrency in applications is bad for the system as a whole. What kind of applications do you have in mind? Windows Forms apps? Console apps? ASP.NET apps? The answer may be different for each kind of app. – Theodor Zoulias Jan 19 '20 at 02:18
  • @TheodorZoulias For simplicity's sake let's imagine the concurrency brings only benefits, imagining that the same thread will process two tasks more optimally using concurrency, than it would do sequentially (...) – Marco Jan 19 '20 at 02:56
  • (...) So, in the first example I understand the expected benefit. Concurrency is visible in these methods. In the second example, there will be no concurrency (at least not from this code with itself). My question is whether this second example makes room for concurrency with other different tasks in progress. If not, there would be no advantage in coding that way. The applications in my case would be ASP.NET and console applications. – Marco Jan 19 '20 at 02:56
  • OK. One more clarification is required. What kind of task is the `DoTask1Async`? Is it a [CPU-bound task or an I/O-bound task](https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean)? The answer may again be different for different kind of tasks. Or you want an answer that covers both of these cases? – Theodor Zoulias Jan 19 '20 at 09:59
  • @TheodorZoulias assume it is 50/50, does some IO, and takes some cpu process. – Marco Jan 19 '20 at 11:27

1 Answers1

1

There are two primary benefits to async/await:

  1. Responsive user interface (for UI apps).
  2. More scalable services (for backend apps).

Both of these benefits are due to the same thing: async/await frees up the calling thread. On the client side, this is a UI thread, so by freeing up the calling thread, the UI remains responsive to the user while (a)waiting for the operation to complete. On the server side, this is a request thread, so by freeing up the request thread, the server hand handle other requests while (a)waiting for the operation to complete.

In your specific example, there's no benefit because Main implies a Console app, which is not a UI app nor a server app. Some developers do use asynchronous code in Console apps for concurrency reasons, but there's no inherit benefit like there is on other platforms.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810