要创建的异步伺机关键字不会造成额外的线程? [英] The async and await keywords don't cause additional threads to be created?

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

问题描述

我很困惑。如何能一个或多个工作并联在单个线程上运行?我理解的并行的显然是错误的。

MSDN的位,我不能绕到我的头:


  

异步和等待关键字不会造成额外的线程是
  创建。异步方法不需要多线程因为异步
  方法并不在自己的线程中运行。该方法对当前运行
  同步上下文和使用时间的线程上,只有当
  方法是有效的。


..和:


  

在开始任务,等待它之间,你就可以开始其他任务。
  的额外任务并行隐含运行,但没有额外的
  线程创建。



解决方案

他们不并行运行,他们轮流。当被阻止正在运行的任务进度,它存储的状态,收益率控制到一个准备好的任务。这是合作多任务,而不是真正的并行处理。

线程在样品原理工作。然而,有我想强调几个关键的不同之处。

首先,仅仅是因为异步 / 等待不是操作系统的线程:


  • 任务将不会看到不同的线程ID

  • 线程局部存储不会自动上下文切换时任务的产量。

其次,在行为差异:


  • 异步 / 等待使用合作多任务的Win32线程使用pre抢占。所以这是必要的,所有的阻塞操作使用异步 / 等待模型进行明确放弃控制权。所以,你可以最终通过阻塞调用不写入产生功能阻塞整个线程及其所有任务。

  • 任务将无法在并行多处理系统上执行。由于重入被控制,这使得保持数据结构一致一大堆容易。

斯蒂芬在评论中指出的那样,你的可以的获得多个操作系统线程同时执行(与所有的复杂性和潜在的竞争条件下沿)如果你使用一个多线程的同步上下文。但MSDN报价大约在单线程上下文情况。

最后,在其他地方使用同样的设计模式,你可以学到很多关于良好做法异步 / 等待通过研究这些:


  • 的Win32的纤维的(使用相同的呼叫风格线程,但合作)

  • 的Win32的重叠I / O 的操作,Linux的的 AIO

  • 协同程序

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

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

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.

.. and:

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.

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

  • Tasks won't see different Thread IDs
  • Thread-local storage is not automatically context-switched when a Task yields.

Secondly, differences in behavior:

  • 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.

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.

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 (uses the same call style as threads, but cooperative)
  • Win32 Overlapped I/O operations, Linux aio
  • Coroutines

这篇关于要创建的异步伺机关键字不会造成额外的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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