基于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.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf/librf.h"
  6. using namespace librf;
  7. //非协程的逻辑线程,或异步代码,可以通过event_t通知到协程,并且不会阻塞协程所在的线程。
  8. static std::thread async_set_event(const event_t & e, std::chrono::milliseconds dt)
  9. {
  10. return std::thread([=]
  11. {
  12. std::this_thread::sleep_for(dt);
  13. e.signal();
  14. });
  15. }
  16. static future_t<> resumable_wait_event(const event_t & e)
  17. {
  18. using namespace std::chrono;
  19. auto result = co_await e.wait();
  20. if (result == false)
  21. std::cout << "time out!" << std::endl;
  22. else
  23. std::cout << "event signal!" << std::endl;
  24. }
  25. static void test_wait_one()
  26. {
  27. using namespace std::chrono;
  28. {
  29. event_t evt;
  30. go resumable_wait_event(evt);
  31. auto tt = async_set_event(evt, 1000ms);
  32. this_scheduler()->run_until_notask();
  33. tt.join();
  34. }
  35. {
  36. event_t evt2(1);
  37. go[&]() -> future_t<>
  38. {
  39. (void)co_await evt2.wait();
  40. std::cout << "event signal on 1!" << std::endl;
  41. };
  42. go[&]() -> future_t<>
  43. {
  44. (void)co_await evt2.wait();
  45. std::cout << "event signal on 2!" << std::endl;
  46. };
  47. std::cout << std::this_thread::get_id() << std::endl;
  48. auto tt = async_set_event(evt2, 1000ms);
  49. this_scheduler()->run_until_notask();
  50. tt.join();
  51. }
  52. }
  53. static void test_wait_three()
  54. {
  55. using namespace std::chrono;
  56. event_t evt1, evt2, evt3;
  57. go[&]() -> future_t<>
  58. {
  59. auto result = co_await event_t::wait_all(std::initializer_list<event_t>{ evt1, evt2, evt3 });
  60. if (result)
  61. std::cout << "all event signal!" << std::endl;
  62. else
  63. std::cout << "time out!" << std::endl;
  64. };
  65. std::vector<std::thread> vtt;
  66. srand((int)time(nullptr));
  67. vtt.emplace_back(async_set_event(evt1, 1ms * (500 + rand() % 1000)));
  68. vtt.emplace_back(async_set_event(evt2, 1ms * (500 + rand() % 1000)));
  69. vtt.emplace_back(async_set_event(evt3, 1ms * (500 + rand() % 1000)));
  70. this_scheduler()->run_until_notask();
  71. for (auto& tt : vtt)
  72. tt.join();
  73. }
  74. #if 0
  75. static void test_wait_any()
  76. {
  77. using namespace std::chrono;
  78. event_t evts[8];
  79. go[&]() -> future_t<>
  80. {
  81. for (size_t i = 0; i < std::size(evts); ++i)
  82. {
  83. intptr_t idx = co_await event_t::wait_any(evts);
  84. std::cout << "event " << idx << " signal!" << std::endl;
  85. }
  86. };
  87. std::vector<std::thread> vtt;
  88. srand((int)time(nullptr));
  89. for (auto & e : evts)
  90. {
  91. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  92. }
  93. this_scheduler()->run_until_notask();
  94. for (auto & tt : vtt)
  95. tt.join();
  96. }
  97. #endif
  98. static void test_wait_all()
  99. {
  100. using namespace std::chrono;
  101. event_t evts[8];
  102. go[&]() -> future_t<>
  103. {
  104. auto result = co_await event_t::wait_all(evts);
  105. if (result)
  106. std::cout << "all event signal!" << std::endl;
  107. else
  108. std::cout << "time out!" << std::endl;
  109. };
  110. std::vector<std::thread> vtt;
  111. srand((int)time(nullptr));
  112. for (auto & e : evts)
  113. {
  114. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  115. }
  116. this_scheduler()->run_until_notask();
  117. for (auto & tt : vtt)
  118. tt.join();
  119. }
  120. static void test_wait_all_timeout()
  121. {
  122. using namespace std::chrono;
  123. event_t evts[8];
  124. go[&]() -> future_t<>
  125. {
  126. auto result = co_await event_t::wait_all_for(1000ms, evts);
  127. if (result)
  128. std::cout << "all event signal!" << std::endl;
  129. else
  130. std::cout << "time out!" << std::endl;
  131. };
  132. std::vector<std::thread> vtt;
  133. srand((int)time(nullptr));
  134. for (auto & e : evts)
  135. {
  136. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  137. }
  138. this_scheduler()->run_until_notask();
  139. for (auto & tt : vtt)
  140. tt.join();
  141. }
  142. void resumable_main_event()
  143. {
  144. test_wait_one();
  145. std::cout << std::endl;
  146. test_wait_three();
  147. std::cout << std::endl;
  148. //test_wait_any();
  149. //std::cout << std::endl;
  150. test_wait_all();
  151. std::cout << std::endl;
  152. test_wait_all_timeout();
  153. std::cout << std::endl;
  154. }
  155. #if LIBRF_TUTORIAL_STAND_ALONE
  156. int main()
  157. {
  158. resumable_main_event();
  159. return 0;
  160. }
  161. #endif