简体   繁体   English

是否等待完全阻止线程?

[英]Does await completely blocks the thread?

When we use await in the code, generally the awaiters captures the context and use that as callback when the awaited task completed successfully. 当我们在代码中使用await时,通常awaiters捕获上下文并在等待的任务成功完成时将其用作回调。 But, since await is generally a No.Op(No Operation), does it make the underlying thread not being reusable until execution of awaited task completes? 但是,由于await通常是No.Op(无操作),它是否会使得底层线程在可以等待任务完成之前不可重用?

For Example 例如

public async Task MethodAAsync()
{
     // Execute some logic
     var test = await MethodB();
    // Execute Remaining logic.
}

Here, since I need the result back to proceed further, I need to await on that. 在这里,由于我需要将结果继续进行,我需要等待。 But, since we are awaiting, does it block the underlying thread resulting thread not being used to execute any tasks? 但是,既然我们正在等待,它是否会阻止底层线程导致线程不被用于执行任何任务?

But, since await is generally a No.Op(No Operation), does it make the underlying thread not being reusable until execution of awaited task completes? 但是,由于await通常是No.Op(无操作),它是否会使得底层线程在可以等待任务完成之前不可重用?

I'm not sure where you got this information from, but await is certainly not a No-Op . 我不确定你从哪里获得这些信息,但await 肯定不是No-Op Calling await on a Task , for example, will invoke a logical call to Task.GetAwaiter , where the TaskAwaiter has to implement INotifyCompletion or ICriticalNotifyCompletion interface, which tells the compiler how to invoke the continuation (everything after the first await ). 例如,对Task调用await将调用对Task.GetAwaiter的逻辑调用,其中TaskAwaiter必须实现INotifyCompletionICriticalNotifyCompletion接口,该接口告诉编译器如何调用continuation(第一次await之后的所有内容)。

The complete structure of async-await transforms your call to a state-machine such that when the state-machine hits the first await , it will first check to see if the called method completed, and if not will register the continuation and return from that method call. async-await的完整结构将您的调用转换为状态机,这样当状态机命中第一个await ,它将首先检查被调用的方法是否已完成,如果没有则将注册延续并从中返回方法调用。 Later, once that method completes, it will re-enter the state-machine in order to complete the method. 之后,一旦该方法完成,它将重新进入状态机以完成该方法。 And that is logically how you see the line after await being hit. 从逻辑上讲,这是你在await被击中之后看到的线条。

But, since we are awaiting, does it block the underlying thread resulting thread not being used to execute any tasks? 但是,既然我们正在等待,它是否会阻止底层线程导致线程不被用于执行任何任务?

No, creating an entire mechanism only to block the calling thread would be useless. 不,创建一个仅阻止调用线程的整个机制将毫无用处。 async-await allow you to actually yield the calling thread back to the caller which allows him to continue execution on that same thread, while the runtime takes care of queuing and invoking the completion. async-await允许您实际将调用线程返回给调用者,这允许他继续在同一个线程上执行,而运行时负责排队并调用完成。

In short, no, it doesn't block your thread. 简而言之,不,它不会阻止你的线程。

More info: http://blog.stephencleary.com/2013/11/there-is-no-thread.html 更多信息: http//blog.stephencleary.com/2013/11/there-is-no-thread.html

The thread has a local queue of Tasks. 该线程具有任务的本地队列。 Once it hits an await it will pick up another Task from it's queue (assuming all methods up it's stack are awaited ). 一旦它命中await它将从它的队列中获取另一个任务(假设所有方法都在awaited它的堆栈)。 If the queue is empty, it will try to get a Task from the global queue, and if it's empty too - the thread will try to get a Task from another thread's own local queue. 如果队列为空,它将尝试从全局队列中获取任务,如果它也是空的 - 线程将尝试从另一个线程自己的本地队列获取任务。 In a simple program, where all threads might get "out of Tasks" - the thread will remain idle until one of the Tasks returns. 在一个简单的程序中,所有线程都可能“脱离任务” - 线程将保持空闲状态,直到其中一个任务返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM