Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "async" run in a separate thread? [duplicate]

Tags:

c#

task

When I call to foo(), is this method runs on a separate thread?

public async Task<bool> foo(){
  //Some code 
}
like image 527
spez Avatar asked Jan 26 '20 11:01

spez


3 Answers

No, it does not. It MAY start another thread internally and return that task, but the general idea is that it does not run on any thread.

Let me explain. The general use of async is if you are not CPU bound and that means IO and all IO in windows has callback operated interfaces at the lowest level, so - a network request sends the request and then goes on with work - but there is no thread attached. At all. The general use case for async is that async runs on a thread and when nothing is there to do it will then use the threads for the tasks completed, allowing multiple operations on one thread - AND... the IO will not use up a thread.

Your method basically turns into a state engine relinquishing control to the task scheduler waiting for a completed task being signalled.

like image 168
TomTom Avatar answered Nov 13 '22 02:11

TomTom


Making the method with async don't mean that it will create another thread. If the CLR is seeing that your method which is called with await in your async method is delayed, it is exiting from that method and waits after awaited methods finish and then continue that method with another thread.

When you invoke a method marked as async, it begins running synchronously on the current thread.

So, the conclusion is that async doesn't create it's own thread. The thread of the calling method is used to execute the async method till it finds an awaitable. The same thread then continues to execute the rest of the calling method beyond the async method call. Within the called async method, after returning from the awaitable, the continuation can be executed on a thread from the thread pool, the only place a separate thread comes into picture.

like image 44
Mihai Alexandru-Ionut Avatar answered Nov 13 '22 00:11

Mihai Alexandru-Ionut


No, the tasks do not imply that there is a separate thread that runs. In the case where your task needs to block on another async task within its method body, it can return execution temporarily while the blocked resource is being waited for, and then returns execution back to complete the task. This is basically what the await keyword is used for. A typical async task is one that waits on I/O or networking resources to return data after a true asynchronous operation has begun. It is also possible that your method itself spawns its own thread, making it asynchronous, but it really just depends on the implementation of the async method itself.

A great explanation is found on this page, in the "cooking breakfast analogy": https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

Note that it is possible to run tasks in parallel, which can spawn separate threads, but when you just call the methods directly, that is not done by default.

like image 1
Andy Mudrak Avatar answered Nov 13 '22 02:11

Andy Mudrak