Asynchronous code, shared variables, thread-pool threads and thread safety

笑着哭i 提交于 2019-12-05 02:59:20

The problem with thread safety is about reading/writing memory. Even when this could continue on a different thread, nothing here is executed concurrent.

I believe this article by Stephen Toub can shed some light on this. In particular, this is a relevant passage about what happens during a context switch:

Whenever code awaits an awaitable whose awaiter says it’s not yet complete (i.e. the awaiter’s IsCompleted returns false), the method needs to suspend, and it’ll resume via a continuation off of the awaiter. This is one of those asynchronous points I referred to earlier, and thus, ExecutionContext needs to flow from the code issuing the await through to the continuation delegate’s execution. That’s handled automatically by the Framework. When the async method is about to suspend, the infrastructure captures an ExecutionContext. The delegate that gets passed to the awaiter has a reference to this ExecutionContext instance and will use it when resuming the method. This is what enables the important “ambient” information represented by ExecutionContext to flow across awaits.

Worth noting that the YieldAwaitable returned by Task.Yield() always returns false.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!