新的 C# 5.0 'async' 和 'await' 关键字是否使用多核? [英] Do the new C# 5.0 'async' and 'await' keywords use multiple cores?

查看:33
本文介绍了新的 C# 5.0 'async' 和 'await' 关键字是否使用多核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加到 C# 5.0 语言的两个新关键字是 asyncawait,两者齐头并进在不阻塞调用线程的情况下异步运行 C# 方法.

Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the calling thread.

我的问题是,这些方法是否真的利用了多个内核并并行运行,或者异步方法是否与调用者在同一线程内核中运行?

My question is, do these methods actually take advantage of multiple cores and run in parallel or does the async method run in the same thread core as the caller?

推荐答案

添加到 C# 5.0 语言中的两个新关键字是 async 和 await,这两个关键字协同工作以异步运行 C# 方法而不会阻塞调用线程.

Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the calling thread.

这违背了该功能的目的,但它对 async/await 功能给予了太多的信任".

That gets across the purpose of the feature, but it gives too much "credit" to the async/await feature.

在这一点上让我非常非常清楚:await 不会神奇地导致同步方法异步运行.它不会启动新线程并运行例如,新线程上的方法.您正在调用的方法必须是知道如何异步运行自身的东西.它如何选择这样做是它的事.

Let me be very, very clear on this point: await does not magically cause a synchronous method to run asynchronously. It does not start up a new thread and run the method on the new thread, for example. The method you are calling has to be the thing that knows how to run itself asynchronously. How it chooses to do so is its business.

我的问题是,这些方法是否真的利用了多个内核并并行运行,或者异步方法是否与调用者在同一线程内核中运行?

My question is, do these methods actually take advantage of multiple cores and run in parallel or does the async method run in the same thread core as the caller?

同样,这完全取决于您调用的方法.await 所做的只是指示编译器将方法重写为可以作为异步任务的延续传递的委托.也就是说,await FooAsync() 的意思是调用FooAsync(),返回的东西一定是代表刚刚启动的异步操作的东西.告诉那个东西,当它知道异步操作已经完成,它应该调用这个委托."委托有一个属性,当它被调用时,当前的方法似乎从它停止的地方"恢复.

Again, that is entirely up to the method you call. All that await does is instruct the compiler to rewrite the method into a delegate that can be passed as the continuation of the asynchronous task. That is, the await FooAsync() means "call FooAsync() and whatever comes back must be something that represents the asynchronous operation that just started up. Tell that thing that when it knows that the asynchronous operation is done, it should call this delegate." The delegate has the property that when it is invoked, the current method appears to resume "where it left off".

如果您调用 schedules 的方法适用于关联到另一个核心的另一个线程,那太好了.如果它启动一个计时器,该计时器将来在 UI 线程上 ping 某些事件处理程序,那就太好了.await 不在乎.它所做的只是确保在异步作业完成后,控制可以从中断的地方恢复.

If the method you call schedules work onto another thread affinitized to another core, great. If it starts a timer that pings some event handler in the future on the UI thread, great. await doesn't care. All it does is makes sure that when the asynchronous job is done, control can resume where it left off.

一个你没有问但可能应该问的问题是:

A question you did not ask but probably should have is:

当异步任务完成并且控制从它停止的地方开始时,是否和以前在同一个线程中执行?

When the asynchronous task is finished and control picks up where it left off, is execution in the same thread as it was before?

这取决于上下文.在 Winforms 应用程序中,您等待来自 UI 线程的某些东西,控制在 UI 线程上再次启动.在控制台应用程序中,可能不会.

It depends on the context. In a winforms application where you await something from the UI thread, control picks up again on the UI thread. In a console application, maybe not.

这篇关于新的 C# 5.0 'async' 和 'await' 关键字是否使用多核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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