Created
February 28, 2020 11:34
-
-
Save modocache/ed7c62f6e570766c0f39b35dad675c2f to your computer and use it in GitHub Desktop.
A C++20 coroutines program that would not be pleasant to debug prior to some improvements I hope to make to LLVM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <experimental/coroutine> | |
using namespace std::experimental; | |
struct coro { | |
struct promise_type { | |
coro get_return_object() { | |
auto handle = coroutine_handle<promise_type>::from_promise(*this); | |
return coro(handle); | |
} | |
suspend_never initial_suspend() { return {}; } | |
suspend_never final_suspend() { return {}; } | |
void return_void() {} | |
void unhandled_exception() {} | |
}; | |
coroutine_handle<promise_type> handle; | |
coro(coroutine_handle<promise_type> handle) | |
: handle(handle) {} | |
}; | |
coro foo() { | |
int i = 0; | |
++i; | |
printf("%d\n", i); // 1 | |
// Breakpoint 1: | |
// (lldb) frame variable i | |
// (int) i = 1 | |
co_await suspend_always(); | |
int j = 0; | |
++i; | |
++j; | |
printf("%d, %d\n", i, j); // 2, 1 | |
// Breakpoint 2: | |
// (lldb) frame variable i | |
// (int) i = <variable not available> | |
// (lldb) frame variable j | |
// (int) j = 1 | |
co_await suspend_always(); | |
++i; | |
++j; | |
printf("%d, %d\n", j, i); // 3, 2 | |
// Breakpoint 3: | |
// (lldb) frame variable i | |
// (int) i = <variable not available> | |
// (lldb) frame variable j | |
// (int) j = <variable not available> | |
} | |
int main() { | |
auto c = foo(); | |
c.handle.resume(); | |
c.handle.resume(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment