使用异步/等待的任务继续并行执行 [英] Task continuation parallel execution with async/await

查看:76
本文介绍了使用异步/等待的任务继续并行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 async/await 结构的控制台应用程序的上下文中,我想知道继续"是否可以在多个主机上并行运行不同CPU上的线程.

In the context of a console application making use of async/await constructs, I would like to know if it's possible for "continuations" to run in parallel on multiple threads on different CPUs.

我认为是这种情况,因为继续发布在默认任务计划程序上(控制台应用程序中没有SynchronizationContext),即线程池.

I think this is the case, as continuations are posted on the default task scheduler (no SynchronizationContext in console app), which is the thread pool.

我知道async/await构造不会构造任何其他线程.仍然应该由线程池为每个CPU构造至少一个线程,因此,如果在线程池上发布了延续,它可以在不同CPU上并行调度任务继续...这就是我的想法,但是由于某些原因,我昨天我对此感到非常困惑,我现在也不确定.

I know that async/await construct do not construct any additional thread. Still there should be at least one thread constructed per CPU by the thread pool, and therefore if continuations are posted on the thread pool, it could schedule task continuations in parrallel on different CPUs ... that's what I thought, but for some reason I got really confused yesterday regarding this and I am not so sure anymore.

这是一些简单的代码:

public class AsyncTest
{
  int i;

  public async Task DoOpAsync()
  {
    await SomeOperationAsync();

    // Does the following code continuation can run 
    // in parrallel ?
    i++;       

    // some other continuation code ....
  }

  public void Start()
  {
    for (int i=0; i<1000; i++)
    { var _ = DoOpAsync(); } // dummy variable to bypass warning
  }
}

SomeOperationAsync 本身不会创建任何线程,而为了举例说明,它只是依靠I/O完成端口异步发送一些请求,因此根本不会阻塞任何线程.

SomeOperationAsync does not create any thread in itself, and let's say for the sake of the example that it just sends some request asynchronously relying on I/O completion port so not blocking any thread at all.

现在,如果我调用将发出1000次异步操作的 Start 方法,那么async方法的继续代码(在等待之后)是否可以在不同的CPU线程上并行运行?即在这种情况下,我是否需要注意线程同步并同步对字段"i"的访问?

Now, if I call Start method which will issue 1000 async operations, is it possible for the continuation code of the async method (after the await) to be run in parallel on different CPU threads ? i.e do I need to take care of thread synchronization in this case and synchronize access to field "i" ?

推荐答案

是的,您应该将线程同步逻辑放在i++周围,因为有可能多个线程将同时在await之后执行代码.

Yes, you should put thread synchronization logic around i++ because it is possible that multiple threads would be executing code after await at the same time.

由于for循环,将创建许多任务.这些任务将在不同的线程池线程上执行.这些任务完成后,继续操作(即等待后的代码)将在不同的线程池线程上再次执行.这样就有可能多个线程同时执行i ++

As a result of your for loop, number of Tasks will be created. These Tasks will be executed on different Thread Pool threads. Once these Tasks are completed the continuation i.e. the code after the await, will be executed again on different Thread Pool threads. This makes it possible that multiple threads would be doing i++ at the same time

这篇关于使用异步/等待的任务继续并行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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