-
Notifications
You must be signed in to change notification settings - Fork 54
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
How was coroutine task rescheduled to the main thread, when resumed in another thread?? #28
Comments
Interesting. What compiler are you using? Could you please try the following code with a little more output? #include <coroutine>
#include <thread>
#include <iostream>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
class MyAwaiter {
public:
bool await_ready() {
return false;
}
void await_suspend(cppcoro::coroutine_handle<> continuation) noexcept {
std::thread([c = continuation]() mutable {
std::cout << "new thread created: " << std::this_thread::get_id() << std::endl;
c.resume(); // resume coroutine in a new thread.
std::cout << "new thread after resume: " << std::this_thread::get_id() << std::endl;
}).detach();
}
void await_resume() {}
};
int main() {
std::cout << "main thread " << std::this_thread::get_id() << std::endl;
auto func = [&]() -> cppcoro::task<> {
std::cout << "coroutine before co_await: " << std::this_thread::get_id() << std::endl;
co_await MyAwaiter();
std::cout << "coroutine after co_await:" << std::this_thread::get_id() << std::endl;
co_return;
};
std::cout << "main thread before sync_wait:" << std::this_thread::get_id() << std::endl;
cppcoro::sync_wait([&]() -> cppcoro::task<> {
std::cout << "top level coroutine start " << std::this_thread::get_id() << std::endl;
auto t = func();
std::cout << "top level coroutine before co_await " << std::this_thread::get_id() << std::endl;
co_await t;
std::cout << "top level coroutine after co_await " << std::this_thread::get_id() << std::endl;
}());
std::cout << "main thread after sync_wait:" << std::this_thread::get_id() << std::endl;
}
On my system I get, as expected:
|
cppcoro lib with this patch:(top commit id is a87e97f)
my compiler is
|
Trying to reproduce, how do I install clang8 on centos7? |
Two ways to get clang8: 1.get clang8 binary from https://releases.llvm.org/download.html#8.0.1 2.build from source code
replace |
output like this
As far as I know, the couroutine task func would resumed in a detached thread, so
after: 140301651535680
id would be the same asnew thread: 140300609353472
.but the output is unexpected, so what happened with
c.resume();
)-:?Thx :-)
The text was updated successfully, but these errors were encountered: