Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if there are no IO threads to handle async result?

Tags:

c#

async-await

I'm wondering what happens when there are no IO threads to handle the result of an async call.

Say you make an async web request (in a server application so all async code is handeled by the thread pool). The OS will signal when there is a result for you and when it does you'll need an IO thread to read the bytes from the socket. If there are no IO threads available because that are all in use (all meaning up to the max set by the thread pool) what happens? Is there a queue where the signal can wait until there is a thread available? Or does the signal just go unheard? If the latter happens, what happens to the code waiting on the await?

like image 556
shortspider Avatar asked Aug 20 '16 11:08

shortspider


People also ask

What happens if we execute an asynchronous method but do not await it?

If you forget to use await while calling an async function, the function starts executing. This means that await is not required for executing the function. The async function will return a promise, which you can use later.

Does async await run on different threads?

Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. 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.

What happens if you dont use await?

What if you don't use await with async ? The call meant to be Asynchronous becomes Synchronous and would immediately impact the system scalability, as threads are now blocked, even worse for a long running IO operations.

Which is the recommended way to wait for an async method to complete?

No problem, just make a loop and call this function with an await: [code] for (int i = pendingList. Count - 1; i >= 0; i--)


2 Answers

Unless you've restricted the thread pool, it will create new I/O threads as necessary. The signal waits in a queue-like structure called an I/O Completion Port (IOCP) until it is retrieved; signals are not lost.

like image 144
Stephen Cleary Avatar answered Oct 07 '22 01:10

Stephen Cleary


Let's go back to earlier times, when OS use to preliminary and did not have much scope of concurrency, then if you open up multiple applications, which needs IO like Excel, Word, Pdf, Ppt, system will appear to be hung and unresponsive, since it cannot deal with all of them together, but if you stop queuing up more request, and system doesn't crash, then you will see all of them get their chance and they get active again.

This is a typical use case of IO requests been queued up waiting for being processed, none of them is lost until and unless the system crash, which doesn't happen nowadays that often, due to robustness of its implementation, remember Blue screen of death, which can still happen and that's the only point when IO requests will be lost.

In fact the essence of Threading in Windows is to introduce robustness even before concurrency, so that nothing that is queued up for processing is lost. At no point of time it will exceed its limit to create more than required number of threads

like image 25
Mrinal Kamboj Avatar answered Oct 06 '22 23:10

Mrinal Kamboj