异步操作等同于同步运行同样的操作后,主叫Task.Wait()立即? [英] Is calling Task.Wait() immediately after an asynchronous operation equivalent to running the same operation synchronously?

查看:202
本文介绍了异步操作等同于同步运行同样的操作后,主叫Task.Wait()立即?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在换句话说,是

var task = SomeLongRunningOperationAsync();
task.Wait();

功能上等同于

SomeLongRunningOperation();

换句话说

,是

var task = SomeOtherLongRunningOperationAsync();
var result = task.Result;

功能上等同于

var result = SomeOtherLongRunningOperation();

根据 Task.Wait和内联,如果​​任务为等待'D上已经开始执行,等待有权阻止。但是,如果它没有开始执行,等待也许能够拉动目标任务到它被排队的调度和内联执行它在当前线程上。

According to Task.Wait and Inlining, if the Task being Wait’d on has already started execution, Wait has to block. However, if it hasn’t started executing, Wait may be able to pull the target task out of the scheduler to which it was queued and execute it inline on the current thread.

那些是2案件决定的任务是要运行哪个线程的只是一个问题,如果你在等待的结果无论如何,有什么关系?

Are those two cases merely a matter of deciding which thread the Task is going to run on, and if you're waiting on the result anyway, does it matter?

有没有使用异步之上,同步形式的任何利益,如果没有异步调用和等待()

Is there any benefit to using the asynchronous form over the synchronous form, if nothing executes between the asynchronous call and the Wait()?

推荐答案

下面有一些区别:


  1. 的计算可能会在不同的线程中运行。如果这个任务是CPU为基础,可内联可能在同一个线程上运行。这就是不确定性。

  2. 如果没有内联发生多一个线程将被使用在计算过程。这通常要花堆栈内存1MB。

  3. 例外将在 AggregateException 包裹。异常堆栈会有所不同。

  4. 任务版本可能死锁如果计算职位,以目前的同步上下文。

  5. 如果线程池刷爆了,这可能死锁如果完成另一个任务的任务必须如期举行。

  6. 线程局部的状态,如 HttpContext.Current (这实际上不是本地线程但几乎)的可能的不同。

  7. 主线程的线程终止将不会到达任务体(除内联的情况下)。我不知道等待自己是否将被中止或没有。

  8. 创建工作诱导内存屏障为何能有一个同步的效果。

  1. The computation might run on a different thread. It might run on the same thread if this task is CPU-based and can be inlined. This is non-deterministic.
  2. If no inlining happens one more thread will be in use during the computation. This usually costs 1MB of stack memory.
  3. Exceptions will be wrapped in AggregateException. The exception stack will be different.
  4. The task version might deadlock if the computation posts to the current synchronization context.
  5. If the thread-pool is maxed out this might deadlock if for the task to complete another task must be scheduled.
  6. Thread-local state, such as HttpContext.Current (which is not actually thread-local but almost), might be different.
  7. A thread abort of the main thread will not reach the task body (except in case of inlining). I'm not sure whether the wait itself will be aborted or not.
  8. Creating a Task induces a memory barrier why can have a synchronizing effect.

请问这件事情?通过这个列表自行决定。

Does this matter? Decide for yourself by this list.

有没有好处,这样做呢?我想不出任何。如果你的计算采用异步IO等待将否定的好处是,异步IO带来的。唯一的例外是扇出IO,例如发行并行10 HTTP请求,并等待着他们。这样,你有10个操作在一个线程的成本。

Are there benefits to doing this? I can't think of any. If your computation uses async IO the wait will negate the benefits that the async IO brings. The one exception would be fan-out IO, e.g. issuing 10 HTTP requests in parallel and waiting for them. That way you have 10 operations at the cost of one thread.

请注意,该等待结果在所有这些方面等价的。

Note, that Wait and Result are equivalent in all these regards.

这篇关于异步操作等同于同步运行同样的操作后,主叫Task.Wait()立即?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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