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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf.h"
  6. using namespace resumef;
  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. if (co_await e.wait() == false)
  20. std::cout << "time out!" << std::endl;
  21. else
  22. std::cout << "event signal!" << std::endl;
  23. }
  24. static void test_wait_one()
  25. {
  26. using namespace std::chrono;
  27. {
  28. event_t evt;
  29. go resumable_wait_event(evt);
  30. auto tt = async_set_event(evt, 1000ms);
  31. this_scheduler()->run_until_notask();
  32. tt.join();
  33. }
  34. {
  35. event_t evt2(1);
  36. go[&]() -> future_t<>
  37. {
  38. (void)co_await evt2.wait();
  39. std::cout << "event signal on 1!" << std::endl;
  40. };
  41. go[&]() -> future_t<>
  42. {
  43. (void)co_await evt2.wait();
  44. std::cout << "event signal on 2!" << std::endl;
  45. };
  46. std::cout << std::this_thread::get_id() << std::endl;
  47. auto tt = async_set_event(evt2, 1000ms);
  48. this_scheduler()->run_until_notask();
  49. tt.join();
  50. }
  51. }
  52. static void test_wait_three()
  53. {
  54. using namespace std::chrono;
  55. event_t evt1, evt2, evt3;
  56. go[&]() -> future_t<>
  57. {
  58. if (co_await event_t::wait_all(std::initializer_list{ evt1, evt2, evt3 }))
  59. std::cout << "all event signal!" << std::endl;
  60. else
  61. std::cout << "time out!" << std::endl;
  62. };
  63. std::vector<std::thread> vtt;
  64. srand((int)time(nullptr));
  65. vtt.emplace_back(async_set_event(evt1, 1ms * (500 + rand() % 1000)));
  66. vtt.emplace_back(async_set_event(evt2, 1ms * (500 + rand() % 1000)));
  67. vtt.emplace_back(async_set_event(evt3, 1ms * (500 + rand() % 1000)));
  68. this_scheduler()->run_until_notask();
  69. for (auto& tt : vtt)
  70. tt.join();
  71. }
  72. static void test_wait_any()
  73. {
  74. using namespace std::chrono;
  75. event_t evts[8];
  76. go[&]() -> future_t<>
  77. {
  78. for (size_t i = 0; i < std::size(evts); ++i)
  79. {
  80. intptr_t idx = co_await event_t::wait_any(evts);
  81. std::cout << "event " << idx << " signal!" << std::endl;
  82. }
  83. };
  84. std::vector<std::thread> vtt;
  85. srand((int)time(nullptr));
  86. for (auto & e : evts)
  87. {
  88. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  89. }
  90. this_scheduler()->run_until_notask();
  91. for (auto & tt : vtt)
  92. tt.join();
  93. }
  94. static void test_wait_all()
  95. {
  96. using namespace std::chrono;
  97. event_t evts[8];
  98. go[&]() -> future_t<>
  99. {
  100. if (co_await event_t::wait_all(evts))
  101. std::cout << "all event signal!" << std::endl;
  102. else
  103. std::cout << "time out!" << std::endl;
  104. };
  105. std::vector<std::thread> vtt;
  106. srand((int)time(nullptr));
  107. for (auto & e : evts)
  108. {
  109. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  110. }
  111. this_scheduler()->run_until_notask();
  112. for (auto & tt : vtt)
  113. tt.join();
  114. }
  115. static void test_wait_all_timeout()
  116. {
  117. using namespace std::chrono;
  118. event_t evts[8];
  119. go[&]() -> future_t<>
  120. {
  121. if (co_await event_t::wait_all_for(1000ms, evts))
  122. std::cout << "all event signal!" << std::endl;
  123. else
  124. std::cout << "time out!" << std::endl;
  125. };
  126. std::vector<std::thread> vtt;
  127. srand((int)time(nullptr));
  128. for (auto & e : evts)
  129. {
  130. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  131. }
  132. this_scheduler()->run_until_notask();
  133. for (auto & tt : vtt)
  134. tt.join();
  135. }
  136. void resumable_main_event()
  137. {
  138. test_wait_one();
  139. std::cout << std::endl;
  140. test_wait_three();
  141. std::cout << std::endl;
  142. test_wait_any();
  143. std::cout << std::endl;
  144. test_wait_all();
  145. std::cout << std::endl;
  146. test_wait_all_timeout();
  147. std::cout << std::endl;
  148. }