-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion for Understanding the promise type #3
Comments
Can co-routine help me writing cps style functions ?I'm trying implement a async program by weaving call-stack to cps style continuation. The most annoying part I found is convert normal function to cps function, which looks like:
will co-routine be able to help me writing functions like this more easily ? |
@kghost Yes, coroutines will be able to help you write functions like this more easily. That is one of their primary use-cases. Instead of passing the continuation in as a parameter to the function, coroutines implicitly provide "the rest of the coroutine" as the continuation when you co_await something. So your example above would become: task<R> function(arguments...) {
body1...
auto otherfunction1_return_value = co_await otherfunction1(arguments...);
body2...
auto otherfunction2_return_value = co_await otherfunction2(arguments...);
body3...
co_return my_return_value;
} The state of the operation is kept as local variables in the coroutine frame which lives until the operation completes, so there is no need to move state into the next continuation/lambda with the coroutine approach. |
Hello, you forget the return value in operator new in the following type: void operator delete(void* ptr, std::size_t size) ... |
Fixed. Thanks @RainerGrimm! |
Just wanted to drop a quick thanks for the write-up. It has been immensely helpful. |
Thanks a lot for putting up the coroutines series to demystify the internals of coroutines. nit: in the "some_coroutine " code example in "Obtaining the return object" section, should the comment "It's constructor takes..." be "Its ..."? |
One Q: In the case where the call is elided, is it possible the calling function is calling regular "operator new" for the coroutine activation frame on the heap? I'm wondering with custom memory allocator, whether there is some kind of real-time guarantee for applications cannot do heap allocation on real-time thread. Thanks. |
you said: But it is not allowed to destroy (using |
The coroutine is not considered suspended until execution reaches the await_suspend() method. So it is undefinded behaviour if you try to destroy() it before that. E.g. in the final_suspend() method. |
my code is like this struct FinalAwaiter : std::suspend_always {
std::coroutine_handle<> await_suspend(std::coroutine_handle<> h) {
auto continuum = m_h.promise().m_continuum;
// we will never resume this coroutine
// the coroutine is `detached` from any task
// we destroy the coroutine by my ourselves
m_h.destroy(); // <- cause double destruct of local variables
if (continuum) {
// co_wait
return continuum;
}
// somebody directly .resume()
return std::noop_coroutine();
}
}; I'm |
No description provided.
The text was updated successfully, but these errors were encountered: