I'm curious about async await threading internals.
Everyone states that async is so much better in case of performance, because it frees threads that are waiting for a response to a long asynchronous call. Ok I get it.
But let's consider this scenario.
I have an async methodA executing an async operation on database. The api of the database exposes function BeginQuery and event QueryCompleted. I wrapped those with a task (with use of TaskCompletionSource).
My question is what is going under the hood between calling BeginQuery and firing event QueryCompleted.
I mean - doesn't it need to spawn some kind of worker to fire the event? At the very low level it must be some synchronous loop that is blocking a thread reading result from db.
What I suppose is that any async call must generate a thread to actually handle the response (maybe wait for it in a low level c++ loop in driver code).
So our only "gain" is that the caller thread can be freed when some other thread is doing its work.
Does calling an asynchronous method always create a new worker thread?
Could someone confirm my understanding?
The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.
Asynchronous Programming vs Multithreading It is a general misconception that both asynchronous programming and multithreading are the same although that's not true. Asynchronous programming is about the asynchronous sequence of Tasks, while multithreading is about multiple threads running in parallel.
From the definitions we just provided, we can see that multithreading programming is all about concurrent execution of different functions. Async programming is about non-blocking execution between functions, and we can apply async with single-threaded or multithreaded programming.
The await keyword, by contrast, is non-blocking, which means the current thread is free to do other things during the wait.
Everyone states that async is so much better in case of performance, because it frees threads that are waiting for a response to a long asynchronous call.
Yes and no. The point behind async
is to free up the calling thread. In UI applications, the primary benefit of async
is responsiveness, because the UI thread is freed up. In server applications, the primary benefit of async
is scalability, because the request thread is freed up to handle other requests.
So our only "gain" is that the caller thread can be freed when some other thread is doing its work. Does always calling an asynchronous method is creating a new worker thread?
No. At the OS level, all I/O is asynchronous. It is the synchronous APIs which block a thread while the underlying asynchronous I/O is in progress. I recently wrote this up in a blog post: there is no thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With