Why is async considered more efficient than multithreading?

I understand both asynchronous and multi-threaded programming, and I did the same and can do it with ease. However, one thing still bothers me: why is there general agreement that asynchronous mode is better than multithreading? ( added: I am saying that any of the approaches is viable and you can make a choice)

At first glance, the reason seems clear - fewer threads, less work for the OS scheduler, less memory wasted for the stack space. But ... I do not feel that these arguments hold water. Let's look at them individually:

  • Less work for OS planner. True, but does that mean less work at all? There are N tasks running in parallel, SOMEBODY should switch between them. It seems to me that we just took work from the kernel of the OS and started doing it in our own user code. But because of this, the amount of work that needs to be done has not changed. Where, then, is efficiency obtained?
  • Less memory is wasted on stack space. Or that?
    • First of all, I do not know about other OSs, but at least on Windows, the stack space for the thread is not executed immediately. Virtual memory addresses are reserved, but actual memory is only committed when necessary.
    • And even if this were done, it would not matter much, because simply allocating memory does not slow down your program. Not unless you have exhausted it, and modern computers have enough memory for thousands of stacks, especially servers.
    • And even if DO stacks are committed and DO eventually causes a lack of memory, most stacks will be used only at the beginning (if your program flirts with a stack overflow, you have big problems to worry about). This means that in any case, it will be possible to lay out most of them.
    • , . , , , - . . , . - Task, , , . , , - , .

... ?

+6
2

, , ? (: , , )

async . , ? "async vs threads" - " ".

- - . , ? , . async , , . async , , .

N , , SOMEBODY .

. -, async, "" . Windows - IOCP , - - "", . " " .

?

"" . , HTTP- , . .. AFAICT , , - ( 10x ). , , , - . , "async threads", "async ".

Node.js , . ASP.NET ( async, , ). Node.js, , , - . 100% . ASP.NET , Core ASP.NET( ) .

+4

( .NET, , async/await)

, , - IO.

:
, . , . , , . , - .
ThreadPool pre Task. threadpool - , , .
Task. , , Task.ContinueWith.

IO - :
, - . , , . . , -. pre Task BeginRead/EndRead BeginWrite/EndWrite, -

Task. - Task.ContinueWith. IO.

Task - , -. Task.ContinueWith. , Task .

Task.ContinueWith .
, a ... . Node.js( JS async/await ). async/ . , # . , , await, -, , , await. ( async/await) ContinueWith .

, async/await + Task ?

  • async/await - . ( , ++, #, Java Javascript, async/await - .)
  • async/await , , , IO. , .
  • -, .
  • Task a IThreadPoolItem .Net. async/await . 1 → .
  • + async/await . . . , , , . , , - , , , .
  • await , . (Microsoft), standarize await ++. 2015 , ++ await , IO- . ( 39:30)

:

. True

False. async/await . . threadpool. async/await , /​​ . , , .

. ?

False. , async/await . varaiables. ( ), .

async , ?

, + async/await . IO - , . + async/await IO-bound-threadpool, . . ( ) . (IO), CPU (json- , ..) (IO ). + async/await , .

, async/await . , . " ", ?

+1

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


All Articles