async 和 await 关键字不会导致创建额外的线程? [英] The async and await keywords don't cause additional threads to be created?

查看:36
本文介绍了async 和 await 关键字不会导致创建额外的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.一个或多个 Task 如何在单个线程上并行运行?我对并行性的理解显然是错误的.

I'm confused. How can one or many Task run in parallel on a single thread? My understanding of parallelism is obviously wrong.

我无法理解的 MSDN 部分:

Bits of MSDN I can't wrap my head around:

async 和 await 关键字不会导致额外的线程创建.异步方法不需要多线程,因为异步方法不在自己的线程上运行.该方法在当前运行同步上下文并仅在线程上使用时间方法处于活动状态.

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.

.. 和:

在开始任务和等待任务之间,您可以开始其他任务.附加任务隐式并行运行,但没有附加任务创建线程.

Between starting a task and awaiting it, you can start other tasks. The additional tasks implicitly run in parallel, but no additional threads are created.

推荐答案

它们不是并行运行,而是轮流运行.当正在运行的任务的进度被阻止时,它会存储其状态并将控制权交给准备好的任务.这是协作式多任务处理,而不是真正的并行性.

They don't run in parallel, they take turns. When progress is blocked for the running Task, it stores its state and yields control to a ready Task. It's cooperative multitasking, not true parallelism.

线程按照示例原则运行.但是,我想强调几个关键差异.

Threads operate on the sample principle. However there are several key differences I'd like to highlight.

首先,仅仅因为 async/await 不是操作系统线程:

First, simply because async/await aren't OS threads:

  • 任务不会看到不同的线程 ID
  • 当任务产生时,线程本地存储不会自动进行上下文切换.

其次,行为上的差异:

  • async/await 使用协作多任务,Win32 线程使用抢占.因此,所有阻塞操作都必须使用 async/await 模型显式地让出控制权.因此,您最终可以通过对未编写为 yield 的函数进行阻塞调用来阻塞整个线程及其所有任务.
  • 任务不会在多处理系统上并行执行.由于可重入受到控制,这使得保持数据结构的一致性变得更加容易.
  • async/await use cooperative multitasking, Win32 threads use pre-emption. So it's necessary that all blocking operations explicitly yield control using the async/await model. So you can end up blocking an entire thread and all its Tasks by making a blocking call to a function not written to yield.
  • Tasks won't be executed in parallel on a multiprocessing system. Since re-entrancy is controlled, this makes keeping data structures consistent a whole lot easier.

正如斯蒂芬在评论中指出的那样,如果您使用多线程同步上下文,您可以在多个操作系统线程中同时执行(以及所有复杂性和潜在的竞争条件).但是 MSDN 引用是关于单线程上下文的情况.

As Stephen points out in a comment, you can get simultaneous execution in multiple OS threads (along with all the complexity and potential race conditions) if you use a multithreaded synchronization context. But the MSDN quotes were about the single-threaded context case.

最后,在其他地方使用了相同的设计范例,您可以通过研究这些来了解关于 async/await 的很多好的做法:

Finally, other places this same design paradigm is used, you can learn a lot about good practices for async/await by studying these:

  • Win32 Fibers(使用与线程相同的调用风格,但需要协作)
  • Win32 重叠 I/O 操作,Linux aio
  • 协程
  • Win32 Fibers (uses the same call style as threads, but cooperative)
  • Win32 Overlapped I/O operations, Linux aio
  • Coroutines

这篇关于async 和 await 关键字不会导致创建额外的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆