123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include <chrono>
- #include <iostream>
- #include <string>
- #include <thread>
-
- #include "librf/librf.h"
-
- using namespace librf;
-
- //请打开结构化异常(/EHa)
- auto async_signal_exception(const intptr_t dividend)
- {
- awaitable_t<int64_t> awaitable;
-
- std::thread([dividend, awaitable]
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
- try
- {
- //也可以注释掉这个判断,使用结构化异常。但就获得不了具体描述信息了
- if (dividend == 0)
- throw std::logic_error("divided by zero");
- awaitable.set_value(10000 / dividend);
- }
- catch (...)
- {
- awaitable.set_exception(std::current_exception());
- }
- }).detach();
-
- return awaitable.get_future();
- }
-
- auto async_signal_exception2(const intptr_t dividend)
- {
- awaitable_t<int64_t> awaitable;
-
- std::thread([dividend, awaitable]
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
- if (dividend == 0)
- awaitable.throw_exception(std::logic_error("divided by zero"));
- else
- awaitable.set_value(10000 / dividend);
- }).detach();
-
- return awaitable.get_future();
- }
-
- future_t<> test_signal_exception()
- {
- for (intptr_t i = 10; i >= 0; --i)
- {
- try
- {
- auto r = co_await async_signal_exception2(i);
- std::cout << "result is " << r << std::endl;
- }
- catch (const std::exception& ex)
- {
- std::cout << "exception signal : " << ex.what() << std::endl;
- }
- catch (...)
- {
- std::cout << "exception signal : who knows?" << std::endl;
- }
- }
- }
-
- future_t<> test_bomb_exception()
- {
- for (intptr_t i = 10; i >= 0; --i)
- {
- auto r = co_await async_signal_exception(i);
- std::cout << "result is " << r << std::endl;
- }
- }
-
- void resumable_main_exception(bool bomb)
- {
- std::cout << __FUNCTION__ << std::endl;
- go test_signal_exception();
- this_scheduler()->run_until_notask();
-
- std::cout << std::endl;
- if (bomb)
- {
- go test_bomb_exception();
- this_scheduler()->run_until_notask();
- }
- }
-
- #if LIBRF_TUTORIAL_STAND_ALONE
- int main()
- {
- resumable_main_exception(true);
- return 0;
- }
- #endif
|