Asynchronous methods do not require additional threads?

MSDN has a paragraph like this:

Asynchronous and pending keywords do not cause additional threads to be created. Asynchronous methods do not require multithreading, since the asynchronous method does not start in its thread. The method works on the current synchronization and uses the time in the stream only when the method is active. You can use Task.Run to move the work associated with the processor to the background thread, but the background thread does not help the process, it just waits for the results to become available.

But it seems to me that I need a little more help with the boldface, as I'm not sure what that means. So, how did async without using Threads ?

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

+4
source share
4 answers

There are many asynchronous operations that do not require multiple threads. Things like asynchronous I / O have interrupts that signal when data is available. This allows you to have an asynchronous call that does not use additional threads - when the signal occurs, the operation ends.

Task.Run can be used to create your own processor-based async methods that will run in a separate thread. This paragraph was to show that this is not the only option.

+11
source

async / await is not just using more threads. This is about using threads that you use more efficiently. When a block of operations, such as waiting for a file to load or read, the async / await pattern allows you to use this existing stream for something else. The compiler processes all the magic plumbing under it, which greatly facilitates its development.

See http://msdn.microsoft.com/en-us/magazine/hh456401.aspx for a description of the problem and the white paper http://www.microsoft.com/en-us/download/details.aspx?id=14058 .

+3
source

Not the code generated by the asynchronous and pending keyword, no. They create code that runs in the current thread if it has a synchronization context. If this is not the case, you are actually getting the threads, but this uses the template for no good reason. The pending expression, what you write on the right side of the await keyword, causes threads to start.

But this stream is often not observed, it may be a stream of device drivers. What are the messages that this is being done with the I / O completion port. Quite often, I / O is always a good reason to use. If you have not imposed WinRT yet, the real reason why async / await is added.

A note on the "presence of a synchronization context." You have one in the stream if the SynchronizationContext.Current property is not null. This almost always takes place in the main thread of the gui application. Also, the only place you usually ever worry about delays doesn't freeze your user interface.

+2
source

Essentially, this is what you execute the async method without calling it with await :

  • Run the method and do as much as possible synchronously.

  • If necessary, pause the method and add it to continue.

  • When the asynchronous part is completed (no longer waiting for it), plan to continue to run on the same thread.

  • No matter what you want, you can work in this thread as usual. You can even check / process the Task returned by the async method.

  • When the thread becomes available, it will run the rest of your method.

The "asynchronous part" can be an IO file, a web request, or just about anything if the calling code can wait for this task to complete. This includes, but is not limited to, a separate stream. As Reed Kopsy noted, there are other ways to perform asynchronous operations, such as interrupts.

+1
source

Source: https://habr.com/ru/post/1432390/


All Articles