基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test_async_exception.cpp 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <thread>
  6. #include "librf.h"
  7. using namespace resumef;
  8. //请打开结构化异常(/EHa)
  9. auto async_signal_exception(const intptr_t dividend)
  10. {
  11. using namespace std::chrono;
  12. resumef::awaitable_t<int64_t> awaitable;
  13. std::thread([dividend, st = awaitable._state]
  14. {
  15. std::this_thread::sleep_for(50ms);
  16. try
  17. {
  18. if (dividend == 0)
  19. throw std::logic_error("divided by zero");
  20. st->set_value(10000 / dividend);
  21. }
  22. catch (...)
  23. {
  24. st->set_exception(std::current_exception());
  25. }
  26. }).detach();
  27. return awaitable;
  28. }
  29. future_vt test_signal_exception()
  30. {
  31. for (intptr_t i = 10; i >= 0; --i)
  32. {
  33. try
  34. {
  35. auto r = co_await async_signal_exception(i);
  36. std::cout << "result is " << r << std::endl;
  37. }
  38. catch (const std::exception& e)
  39. {
  40. std::cout << "exception signal : " << e.what() << std::endl;
  41. }
  42. catch (...)
  43. {
  44. std::cout << "exception signal : who knows?" << std::endl;
  45. }
  46. }
  47. }
  48. future_vt test_bomb_exception()
  49. {
  50. for (intptr_t i = 10; i >= 0; --i)
  51. {
  52. auto r = co_await async_signal_exception(i);
  53. std::cout << "result is " << r << std::endl;
  54. }
  55. }
  56. void resumable_main_exception()
  57. {
  58. go test_signal_exception();
  59. g_scheduler.run_until_notask();
  60. std::cout << std::endl;
  61. go test_bomb_exception();
  62. g_scheduler.run_until_notask();
  63. }