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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include <thread>
  7. #include "librf.h"
  8. using namespace resumef;
  9. //非协程的逻辑线程,或异步代码,可以通过event_t通知到协程,并且不会阻塞协程所在的线程。
  10. std::thread async_set_event(const event_t & e, std::chrono::milliseconds dt)
  11. {
  12. return std::thread([=]
  13. {
  14. std::this_thread::sleep_for(dt);
  15. e.signal();
  16. });
  17. }
  18. future_t<> resumable_wait_event(const event_t & e)
  19. {
  20. using namespace std::chrono;
  21. if (co_await e.wait() == false)
  22. std::cout << "time out!" << std::endl;
  23. else
  24. std::cout << "event signal!" << std::endl;
  25. }
  26. void test_wait_one()
  27. {
  28. using namespace std::chrono;
  29. {
  30. event_t evt;
  31. go resumable_wait_event(evt);
  32. auto tt = async_set_event(evt, 1000ms);
  33. this_scheduler()->run_until_notask();
  34. tt.join();
  35. }
  36. {
  37. event_t evt2(1);
  38. go[&]() -> future_t<>
  39. {
  40. (void)co_await evt2.wait();
  41. std::cout << "event signal on 1!" << std::endl;
  42. };
  43. go[&]() -> future_t<>
  44. {
  45. (void)co_await evt2.wait();
  46. std::cout << "event signal on 2!" << std::endl;
  47. };
  48. std::cout << std::this_thread::get_id() << std::endl;
  49. auto tt = async_set_event(evt2, 1000ms);
  50. this_scheduler()->run_until_notask();
  51. tt.join();
  52. }
  53. }
  54. void test_wait_any()
  55. {
  56. using namespace std::chrono;
  57. event_t evts[8];
  58. go[&]() -> future_t<>
  59. {
  60. for (size_t i = 0; i < _countof(evts); ++i)
  61. {
  62. intptr_t idx = co_await event_t::wait_any(evts);
  63. std::cout << "event " << idx << " signal!" << std::endl;
  64. }
  65. };
  66. std::vector<std::thread> vtt;
  67. srand((int)time(nullptr));
  68. for (auto & e : evts)
  69. {
  70. vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
  71. }
  72. this_scheduler()->run_until_notask();
  73. for (auto & tt : vtt)
  74. tt.join();
  75. }
  76. void test_wait_all()
  77. {
  78. using namespace std::chrono;
  79. event_t evts[8];
  80. go[&]() -> future_t<>
  81. {
  82. if (co_await event_t::wait_all(evts))
  83. std::cout << "all event signal!" << std::endl;
  84. else
  85. std::cout << "time out!" << std::endl;
  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. void test_wait_all_timeout()
  98. {
  99. using namespace std::chrono;
  100. event_t evts[8];
  101. go[&]() -> future_t<>
  102. {
  103. if (co_await event_t::wait_all_for(1000ms, evts))
  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. void resumable_main_event()
  119. {
  120. test_wait_one();
  121. std::cout << std::endl;
  122. test_wait_any();
  123. std::cout << std::endl;
  124. test_wait_all();
  125. std::cout << std::endl;
  126. test_wait_all_timeout();
  127. std::cout << std::endl;
  128. }