-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
当调用 .detach(), 然后不等待它,那么这个 detch 的协程就直接后台跑了。就好像 create_thread 创建了个新线程一样。 当调用 .detach(), 然后使用 co_await 等待它,那么当前协程就等待这个 detach 的协程完成。 就好像调用 std.thread.join() 那样。
- Loading branch information
Showing
4 changed files
with
198 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
add_executable(test2 test2.cpp) | ||
target_link_libraries(test2 ucoro) | ||
|
||
add_test(NAME test2 COMMAND test2) | ||
set_target_properties(test2 PROPERTIES FOLDER "ucoro_tests") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <iostream> | ||
#include "ucoro/awaitable.hpp" | ||
|
||
|
||
ucoro::awaitable<int> coro_compute_int(int value) | ||
{ | ||
co_return (value * 100); | ||
} | ||
|
||
ucoro::awaitable<void> coro_compute_exec(int value) | ||
{ | ||
auto x = co_await ucoro::local_storage; | ||
std::cout << "local storage: " << std::any_cast<std::string>(x) << std::endl; | ||
|
||
try | ||
{ | ||
auto y = co_await ucoro::local_storage_t<std::string>(); | ||
std::cout << "local storage: " << y << std::endl; | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
std::cout << e.what(); | ||
} | ||
|
||
auto comput_promise = coro_compute_int(value); | ||
|
||
auto ret = co_await std::move(comput_promise); | ||
std::cout << "return: " << ret << std::endl; | ||
} | ||
|
||
ucoro::awaitable<void> coro_compute() | ||
{ | ||
for (auto i = 0; i < 100; i+=2) | ||
{ | ||
co_await coro_compute_exec(i); | ||
co_await coro_compute_exec(i+1).detach(std::string{"hello from detached coro"}); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
std::string str = "hello from main coro"; | ||
|
||
coro_start(coro_compute(), str); | ||
|
||
return 0; | ||
} |