基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_async_event_v2.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf.h"
  6. using namespace resumef;
  7. using namespace std::chrono;
  8. //非协程的逻辑线程,或异步代码,可以通过event_t通知到协程,并且不会阻塞协程所在的线程。
  9. static std::thread async_set_event_all(const event_v2::event_t & e, std::chrono::milliseconds dt)
  10. {
  11. return std::thread([=]
  12. {
  13. std::this_thread::sleep_for(dt);
  14. e.signal_all();
  15. });
  16. }
  17. static std::thread async_set_event_one(event_v2::event_t e, std::chrono::milliseconds dt)
  18. {
  19. return std::thread([=]
  20. {
  21. std::this_thread::sleep_for(dt);
  22. e.signal();
  23. });
  24. }
  25. static future_t<> resumable_wait_event(event_v2::event_t e, int idx)
  26. {
  27. if (co_await e)
  28. std::cout << "[" << idx << "]event signal!" << std::endl;
  29. else
  30. std::cout << "[" << idx << "]time out!" << std::endl;
  31. }
  32. static future_t<> resumable_wait_timeout(event_v2::event_t e, milliseconds dt, int idx)
  33. {
  34. if (co_await e.wait_for(dt))
  35. std::cout << "[" << idx << "]event signal!" << std::endl;
  36. else
  37. std::cout << "[" << idx << "]time out!" << std::endl;
  38. }
  39. static void test_notify_all()
  40. {
  41. event_v2::event_t evt;
  42. go resumable_wait_event(evt, 0);
  43. go resumable_wait_event(evt, 1);
  44. go resumable_wait_event(evt, 2);
  45. auto tt = async_set_event_all(evt, 100ms);
  46. this_scheduler()->run_until_notask();
  47. tt.join();
  48. }
  49. static void test_notify_one()
  50. {
  51. using namespace std::chrono;
  52. {
  53. event_v2::event_t evt;
  54. go resumable_wait_event(evt, 10);
  55. go resumable_wait_event(evt, 11);
  56. go resumable_wait_event(evt, 12);
  57. auto tt1 = async_set_event_one(evt, 100ms);
  58. auto tt2 = async_set_event_one(evt, 500ms);
  59. auto tt3 = async_set_event_one(evt, 800ms);
  60. this_scheduler()->run_until_notask();
  61. tt1.join();
  62. tt2.join();
  63. tt3.join();
  64. }
  65. }
  66. static void test_wait_all_timeout()
  67. {
  68. using namespace std::chrono;
  69. srand((int)time(nullptr));
  70. event_v2::event_t evts[10];
  71. std::vector<std::thread> vtt;
  72. for(size_t i = 0; i < _countof(evts); ++i)
  73. {
  74. go resumable_wait_timeout(evts[i], 100ms, (int)i);
  75. vtt.emplace_back(async_set_event_one(evts[i], 1ms * (50 + i * 10)));
  76. }
  77. this_scheduler()->run_until_notask();
  78. for (auto& tt : vtt)
  79. tt.join();
  80. }
  81. void resumable_main_event_v2()
  82. {
  83. test_notify_all();
  84. std::cout << std::endl;
  85. test_notify_one();
  86. std::cout << std::endl;
  87. test_wait_all_timeout();
  88. std::cout << std::endl;
  89. }