[英]C# async await and threadpool
I understand that when using async and await on a method called on the UI thread that the UI thread can be freed to do other work while awaiting some IO to complete. 我理解当在UI线程上调用的方法上使用async和await时,可以释放UI线程以在等待某些IO完成时执行其他工作。 If I use the async await on a method called by a threadpool thread what happens to the that threadpool thread while the IO completes? 如果我在线程池线程调用的方法上使用async await,那么在IO完成时该线程池线程会发生什么? Is it returned to the pool? 它回到了游泳池吗? When the IO is completed what thread completes the method in this latter case? 当IO完成后,在后一种情况下哪个线程完成了该方法?
In that case, the continuation executes on any available thread-pool thread. 在这种情况下,继续在任何可用的线程池线程上执行。
In theory it's up to the awaitable to schedule the continuation appropriately, but generally an awaitable captures the current SynchronizationContext
when the async method passes in the continuation, and uses that synchronization context to schedule the continuation when the awaitable has completed. 从理论上讲,可以适当地安排延续,但通常等待当异步方法在延续中传递时捕获当前的SynchronizationContext
,并使用该同步上下文在等待完成时安排继续。 (Module ConfigureAwait
calls etc.) (模块ConfigureAwait
调用等)
In the case of the thread pool, there is no synchronization context, so the continuation is just scheduled on any thread pool thread. 在线程池的情况下, 没有同步的内容,这样的延续只是安排在任何线程池中的线程。 (The same is true on a console app's main thread - or basically any thread where a synchronization context hasn't been set.) (在控制台应用程序的主线程上也是如此 - 或者基本上没有设置同步上下文的任何线程。)
If I use the async await on a method called by a threadpool thread what happens to the that threadpool thread while the IO completes? 如果我在线程池线程调用的方法上使用async await,那么在IO完成时该线程池线程会发生什么? Is it returned to the pool? 它回到了游泳池吗?
Yes, the thread pool thread is returned to the thread pool. 是的,线程池线程返回到线程池。
When the IO is completed what thread completes the method in this latter case? 当IO完成后,在后一种情况下哪个线程完成了该方法?
By default, await
will capture a current "context", and use that to resume the async
method when the await
completes. 默认情况下, await
将捕获当前的“上下文”,并在await
完成时使用它来恢复async
方法。 This "context" is SynchronizationContext.Current
unless it is null
, in which case the "context" is TaskScheduler.Current
. 这个“上下文”是SynchronizationContext.Current
除非它为null
,在这种情况下“context”是TaskScheduler.Current
。
In the UI case, there's a UI SynchronizationContext
that causes the async
method to resume executing on the UI thread. 在UI情况下,有一个UI SynchronizationContext
,它使async
方法在UI线程上继续执行。 In the thread pool case, SynchronizationContext.Current
is null
, and TaskScheduler.Current
is the thread pool task scheduler. 在线程池的情况下, SynchronizationContext.Current
为null
, TaskScheduler.Current
是线程池任务调度程序。 So, the async
method is resumed on a thread pool thread (any arbitrary thread, not necessarily the same thread). 因此, async
方法在线程池线程(任意线程,不一定是同一线程)上恢复。
If you want more information on this behavior, I have an async
intro that you may find helpful. 如果您想了解有关此行为的更多信息,我有一个您可能会发现有用的async
介绍 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.