Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::async vs async/await in C#

I'm wondering if the new c++ feature std::async is quite comparable to the two C# keywords async/await or not and if not why?

like image 318
Guillaume Paris Avatar asked Mar 31 '11 10:03

Guillaume Paris


People also ask

Is there async await in C++?

Async and await in C++ helps in writing asynchronous code simply. Calculation and getting data from I/O is an example for Async/await operations.

What is the difference between await and async?

await can only be used in async functions. It is used for calling an async function and waits for it to resolve or reject. await blocks the execution of the code within the async function in which it is located.

Is C sharp asynchronous?

C# has a language-level asynchronous programming model, which allows for easily writing asynchronous code without having to juggle callbacks or conform to a library that supports asynchrony. It follows what is known as the Task-based Asynchronous Pattern (TAP).

What is the difference between async and await in C#?

An async keyword is a method that performs asynchronous tasks such as fetching data from a database, reading a file, etc, they can be marked as “async”. Whereas await keyword making “await” to a statement means suspending the execution of the async method it is residing in until the asynchronous task completes.


2 Answers

Not really, assuming I'm reading this std::async documentation correctly.

C# 5's async/await feature involves a complex compiler transformation of the asynchronous method so that you can write code which looks pretty much synchronous, but has points of asynchrony. The compiler builds a state machine for you, creates appropriate callbacks etc.

EDIT: While I previously believed that std::async simply forced you to pass in a callback explicitly, it looks like it's even more primitive than that. Either way, I believe it's mostly/completely a library feature whereas C# 5's asynchronous methods are mostly a language feature with library support.

EDIT: As noted further in comments, it looks like it's on its way for VC++...

like image 165
Jon Skeet Avatar answered Oct 18 '22 18:10

Jon Skeet


CPPASYNC (provided in another answer) looks like what you are looking for. The "Async" part is easy and the performance looks good (likely better than the C# implementation). It's ugly b/c you need special "Await" wrappers around async callback calls. Some Boost networking is provided, and they're easy to make, but you can't just "Await" anything: Async any method/function,

Within an Async function, await either:
- An Async function
- An await wrapper (simple to make) around an asynchronous function (that takes a callback)

like image 30
user1212212 Avatar answered Oct 18 '22 18:10

user1212212