Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async methods don't require additional threads?

In MSDN, there is a paragraph like this:

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. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available.

But it looks I need little more help with the bold text since I am not sure what it exactly means. So how come it becomes async without using Threads?

Source: http://msdn.microsoft.com/en-us/library/hh191443.aspx

like image 518
Tarik Avatar asked Dec 04 '22 02:12

Tarik


1 Answers

There are many asynchronous operations which don't require the use of multiple threads. Things like Asynchronous IO work by having interrupts which signal when data is available. This allows you to have an asynchronous call which isn't using extra threads - when the signal occurs, the operation completes.

Task.Run can be used to make your own CPU-based async methods, which will run on its own separate thread. The paragraph was intended to show that this isn't the only option, however.

like image 200
Reed Copsey Avatar answered Dec 30 '22 07:12

Reed Copsey