0

Does async await always use thread pool? I really don't understand how this works. Some example, i made an async request to database, let's imagine its postgresql, with postgresql driver. I made that request, the request go to the driver and send to the database itself, like many books says, the thread pool (Go to do other work). But who and how is waiting for the response of the database? Do we need other thread to wait for the response or what?

Ok another question like this too, we make a call to some text file using some async method, he go and read that file, thread "again go do some other things", so who here wait for returned result, does this use thread for waiting the results or some other mechanism working on it? Would be happy for detailed answer :)

jancamra
  • 1
  • 1
  • _"Does async await always use thread pool?"_ -- since there exists [`Task.CompletedTask`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.completedtask#System_Threading_Tasks_Task_CompletedTask) and [`Task.FromResult()`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.fromresult#System_Threading_Tasks_Task_FromResult__1___0_), obviously the thread pool isn't _always_ used. Beyond that, there are lots of scenarios where some mechanism besides the thread pool is used for asynchrony (callbacks being the other significant example). See duplicates. – Peter Duniho Jun 30 '21 at 23:43
  • "*I really don't understand how this works*" although this question seems to have a definite domain, its overly broad and something that is not easily explained. However, there are plenty of explanations around, see Stephen Cleary... – TheGeneral Jun 30 '21 at 23:49
  • 1
    `async/await` by itself doesn't use anything, it's just a syntactic sugar that puts everything after `await` in a callback. It's up to a method that you're awating to decide if it runs on the thread pool, new tread, current thread, or no thread at all in case of asynchronous I/O. – Yarik Jul 01 '21 at 00:05
  • 1
    [There is no thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html). This article is the most detailed answer you could wish for. – Theodor Zoulias Jul 01 '21 at 04:45
  • I recommend starting with my [async intro](https://blog.stephencleary.com/2012/02/async-and-await.html) and [best practices](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming) for practical coding guidelines, and then follow up with [there is no thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html) for a deeper explanation. – Stephen Cleary Jul 01 '21 at 14:24

0 Answers0