基于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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. static void test_wait_any()
  75. {
  76. using namespace std::chrono;
  77. event_t evts[8];
  78. go[&]() -> future_t<>
  79. {
  80. for (size_t i = 0; i < std::size(evts); ++i)
  81. {
  82. intptr_t idx = co_await event_t::wait_any(evts);
  83. std::cout << "event " << idx << " signal!" << std::endl;
  84. }
  85. };
  86. std::vector<std::thread> vtt;
  87. srand((int)time(nullptr));
  88. for (auto & e : evts)
  89. {
  90. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  91. }
  92. this_scheduler()->run_until_notask();
  93. for (auto & tt : vtt)
  94. tt.join();
  95. }
  96. static void test_wait_all()
  97. {
  98. using namespace std::chrono;
  99. event_t evts[8];
  100. go[&]() -> future_t<>
  101. {
  102. auto result = co_await event_t::wait_all(evts);
  103. if (result)
  104. std::cout << "all event signal!" << std::endl;
  105. else
  106. std::cout << "time out!" << std::endl;
  107. };
  108. std::vector<std::thread> vtt;
  109. srand((int)time(nullptr));
  110. for (auto & e : evts)
  111. {
  112. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  113. }
  114. this_scheduler()->run_until_notask();
  115. for (auto & tt : vtt)
  116. tt.join();
  117. }
  118. static void test_wait_all_timeout()
  119. {
  120. using namespace std::chrono;
  121. event_t evts[8];
  122. go[&]() -> future_t<>
  123. {
  124. auto result = co_await event_t::wait_all_for(1000ms, evts);
  125. if (result)
  126. std::cout << "all event signal!" << std::endl;
  127. else
  128. std::cout << "time out!" << std::endl;
  129. };
  130. std::vector<std::thread> vtt;
  131. srand((int)time(nullptr));
  132. for (auto & e : evts)
  133. {
  134. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  135. }
  136. this_scheduler()->run_until_notask();
  137. for (auto & tt : vtt)
  138. tt.join();
  139. }
  140. void resumable_main_event()
  141. {
  142. test_wait_one();
  143. std::cout << std::endl;
  144. test_wait_three();
  145. std::cout << std::endl;
  146. test_wait_any();
  147. std::cout << std::endl;
  148. test_wait_all();
  149. std::cout << std::endl;
  150. test_wait_all_timeout();
  151. std::cout << std::endl;
  152. }
  153. int main()
  154. {
  155. resumable_main_event();
  156. return 0;
  157. }