Why do we need the async keyword?

I just started playing around with async/await in .Net 4.5. One thing I’m initially curious about, why is the async keyword necessary? The explanation I read was that it is a marker so the compiler knows a method awaits something. But it seems like the compiler should be able to figure this out without a keyword. So what else does it do?

0

There are several answers here, and all of them talk about what async methods do, but none of them answer the question, which is why async is needed as a keyword that goes in the function declaration.

It’s not “to direct the compiler to transform the function in a special way”; await alone could do that. Why? Because C# already has another mechanism where the presence of a special keyword in the method body causes the compiler to perform extreme (and very similar to async/await) transformations on the method body: yield.

Except that yield isn’t its own keyword in C#, and understanding why will explain async as well. Unlike in most languages that support this mechanism, in C# you can’t say yield value; You have to say yield return value; instead. Why? Because it was added in to the language after C# already existed, and it was quite reasonable to assume that someone, somewhere, might have used yield as the name of a variable. But because there was no pre-existing scenario in which <variable name> return was syntactically correct, yield return got added to the language to make it possible to introduce generators while maintaining 100% backwards compatibility with existing code.

And this is why async was added as a function modifier: to avoid breaking existing code that used await as a variable name. Since no async methods already existed, no old code is invalidated, and in new code, the compiler can use the presence of the async tag to know that await should be treated as a keyword and not an identifier.

3

it changes the method from a normal method to a object with callback which requires a totally different approach for code generation

and when something drastic like that happens it is customary to signify it clearly (we learned that lesson from C++)

5

The whole idea with keywords like “async” or “unsafe” is to remove ambiguity as to how the code they modify should be treated. In the case of the async keyword, it tells the compiler to treat the method modified as something that does not need to return immediately. This allows for the thread where this method is used to continue without having to wait on the results of that method. It’s effectively a code optimization.

3

OK, here is my take on it.

There is something called coroutines that has been known for decades. (“Knuth and Hopper”-class “for decades”) They are generalizations of subroutines, in such as not only do they get and release control at function start and return statement, but they also do it at specific points (suspension points). A subroutine is a coroutine with no suspension points.

They are PLAIN EASY to implement with C macros, as shown in the following paper about “protothreads”. (http://dunkels.com/adam/dunkels06protothreads.pdf) Read it. I’ll wait…

The bottom line of this is that the macros create a big switch, and a case label at each suspension point. At each suspension point, the function stores the value of the immediately following case label, so that it knows where to resume execution next time it is called. And it returns control to the caller.

This is done without modifying the apparent flow of control of the code described in the “protothread”.

Imagine now that you have a big loop calling all these “protothreads” in turn, and you get concurrently executing “protothreads” on a single thread.

This approach has two drawbacks:

  1. You cannot keep state in local variables between resumptions.
  2. You cannot suspend the “protothread” from an arbitrary call depth. (all suspension points must be at level 0)

There are workarounds for both:

  1. All local variables must be pulled up to the context of the protothread (context which is already needed by the fact the protothread must store its next resumption point)
  2. If you feel you really need to call another protothread from a protothread, “spawn” a child protothread and suspend until completion of the child.

And if you had compiler support to do the rewrite work that the macros and workaround do, well, you could just write your protothread code just as you intend and insert suspension points with a keyword.

And this is what async and await are all about: creating (stackless) coroutines.

The coroutines in C# are reified as objects of (generic or non-generic) class Task.

I find these keywords very misleading. My mental reading is:

  • async as “suspensible”
  • await as “suspend until completion of”
  • Task as “future …”

Now. Do we really need to mark the function async? Apart from saying that it should trigger the code rewrite mechanisms to make the function a coroutine, it resolves some ambiguities. Consider this code.

public Task<object> AmIACoroutine() {
    var tcs = new TaskCompletionSource<object>();
    return tcs.Task;
}

Assuming that async is not mandatory, is this a coroutine or a normal function? Should the compiler rewrite it as a coroutine or not? Both could be possible with different eventual semantics.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật