Is the new C# async feature implemented strictly in the compiler

邮差的信 提交于 2019-12-01 16:46:43

问题


As a C# programmer whose interested in exploring "how things work", i am interested in understanding a bit more about the process that makes the new async feature work.

I have followed Eric Lippert's excellent article series on async: Async blog posts

I don't remember seeing anywhere any reference to the implementation of this feature (in a high level) except for the fact the the "compiler is doing most of the work" for us.

Is this feature strictly a compiler feature then? does the compiler rewrite the code in some manner and that's it? or are there other things like runtime support that make this happen?


回答1:


No, it's not entirely in the compiler. It relies on some new interfaces such as INotifyCompletion and some framework implementation support such as AsyncTaskMethodBuilder. I don't believe there are any CLR changes required though.

The compiler does a lot of work, building a state machine - it just refers to a few of those types within the state machine. Oh, and a lot of the Task-related types were significantly modified primarily for performance reasons.

I have a series of blog posts which were initially written against the CTP but only using vanilla .NET 4 and some classes I whipped up myself. They won't work against the production implementation (as things changed a bit over time) but they'll give you a general impression of what's going on behind the scenes.



来源:https://stackoverflow.com/questions/15349287/is-the-new-c-sharp-async-feature-implemented-strictly-in-the-compiler

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