-
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.
解决老中医提出的不能 syncAwait 然后捕获异常的问题。测试用例见 test7
简单的来说,就是 ```c++ try { start_coro(some_coro_that_throw()); } catch(...) { 这里抓不到异常 } ``` 原因是 start_coro 实际上同 asio 的 co_spawn 一样,分叉了。 这样就没办法抓异常了,除非修改some_coro_that_throw()本身的代码。 如果是 ```c++ co_await start_coro(some_coro_that_throw()); ``` 固然这样写,就抓的到异常了。但是在非协程上下文里,就无法使用了。 因此需要一种在非协程上下文环境里,同步等待协程的方式,并且能抓到异常。 所以这个补丁就是新增了一个 sync_await() 函数。具体使用就是 ```c++ void 常规函数() { try { sync_await(some_coro_that_throw()); } catch(...) { 这里能抓到异常 } } ```
- Loading branch information
Showing
4 changed files
with
138 additions
and
5 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(test7 test.cpp) | ||
target_link_libraries(test7 ucoro) | ||
|
||
add_test(NAME test7 COMMAND test7) | ||
set_target_properties(test7 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,45 @@ | ||
|
||
#include "ucoro/awaitable.hpp" | ||
#include <iostream> | ||
|
||
ucoro::awaitable<int> test() | ||
{ | ||
throw std::runtime_error("test throw"); | ||
co_return 1; | ||
} | ||
|
||
|
||
ucoro::awaitable<void> test2() | ||
{ | ||
throw std::runtime_error("test throw"); | ||
co_return; | ||
} | ||
|
||
ucoro::awaitable<int> coro_compute() | ||
{ | ||
try | ||
{ | ||
sync_await(test2()); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
std::cerr << "exception in test2: " << e.what() << '\n'; | ||
} | ||
|
||
co_return co_await test(); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
try | ||
{ | ||
std::string str = "hello"; | ||
sync_await(coro_compute(), str); | ||
} | ||
catch (std::exception& e) | ||
{ | ||
std::cerr << "exception: " << e.what() << std::endl; | ||
} | ||
|
||
return 0; | ||
} |