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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <thread>
  6. #include "librf.h"
  7. using namespace resumef;
  8. future_vt resumalbe_set_event(const event_t & e, std::chrono::milliseconds dt)
  9. {
  10. co_await resumef::sleep_for(dt);
  11. e.signal();
  12. std::cout << "+";
  13. }
  14. void async_set_event(const event_t & e, std::chrono::milliseconds dt)
  15. {
  16. std::thread([=]
  17. {
  18. std::this_thread::sleep_for(dt);
  19. e.signal();
  20. }).detach();
  21. }
  22. void test_wait_timeout_one()
  23. {
  24. std::cout << __FUNCTION__ << std::endl;
  25. using namespace std::chrono;
  26. event_t evt;
  27. go [&evt]() -> future_vt
  28. {
  29. intptr_t counter = 0;
  30. for (;;)
  31. {
  32. if (co_await evt.wait_for(500ms))
  33. break;
  34. ++counter;
  35. std::cout << ".";
  36. }
  37. std::cout << counter << std::endl;
  38. };
  39. async_set_event(evt, 10s + 50ms);
  40. //go resumalbe_set_event(evt, 10s + 50ms);
  41. this_scheduler()->run_until_notask();
  42. }
  43. void test_wait_timeout_any_invalid()
  44. {
  45. std::cout << __FUNCTION__ << std::endl;
  46. using namespace std::chrono;
  47. event_t evts[8];
  48. //无效的等待
  49. go[&]()-> future_vt
  50. {
  51. intptr_t idx = co_await event_t::wait_any_for(500ms, std::begin(evts), std::end(evts));
  52. assert(idx < 0);
  53. std::cout << "invalid wait!" << std::endl;
  54. };
  55. this_scheduler()->run_until_notask();
  56. }
  57. void test_wait_timeout_any()
  58. {
  59. std::cout << __FUNCTION__ << std::endl;
  60. using namespace std::chrono;
  61. event_t evts[8];
  62. go[&]() -> future_vt
  63. {
  64. intptr_t counter = 0;
  65. for (;;)
  66. {
  67. intptr_t idx = co_await event_t::wait_any_for(500ms, evts);
  68. if (idx >= 0)
  69. {
  70. std::cout << counter << std::endl;
  71. std::cout << "event " << idx << " signal!" << std::endl;
  72. break;
  73. }
  74. ++counter;
  75. std::cout << ".";
  76. }
  77. //取消剩下的定时器,以便于协程调度器退出来
  78. this_scheduler()->timer()->clear();
  79. };
  80. srand((int)time(nullptr));
  81. for (auto & e : evts)
  82. {
  83. //go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  84. async_set_event(e, 1ms * (1000 + rand() % 5000));
  85. }
  86. this_scheduler()->run_until_notask();
  87. }
  88. void test_wait_timeout_all_invalid()
  89. {
  90. std::cout << __FUNCTION__ << std::endl;
  91. using namespace std::chrono;
  92. event_t evts[8];
  93. //无效的等待
  94. go[&]()-> future_vt
  95. {
  96. bool result = co_await event_t::wait_all_for(500ms, std::begin(evts), std::end(evts));
  97. assert(!result);
  98. std::cout << "invalid wait!" << std::endl;
  99. };
  100. this_scheduler()->run_until_notask();
  101. }
  102. void test_wait_timeout_all()
  103. {
  104. std::cout << __FUNCTION__ << std::endl;
  105. using namespace std::chrono;
  106. event_t evts[8];
  107. go[&]() -> future_vt
  108. {
  109. intptr_t counter = 0;
  110. for (;;)
  111. {
  112. if (co_await event_t::wait_all_until(system_clock::now() + 500ms, evts))
  113. {
  114. std::cout << counter << std::endl;
  115. std::cout << "all event signal!" << std::endl;
  116. break;
  117. }
  118. ++counter;
  119. std::cout << ".";
  120. }
  121. };
  122. srand((int)time(nullptr));
  123. for (auto & e : evts)
  124. {
  125. go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  126. //async_set_event(e, 1ms * (1000 + rand() % 5000));
  127. }
  128. this_scheduler()->run_until_notask();
  129. }
  130. void resumable_main_event_timeout()
  131. {
  132. using namespace std::chrono;
  133. test_wait_timeout_one();
  134. std::cout << std::endl;
  135. test_wait_timeout_any_invalid();
  136. std::cout << std::endl << std::endl;
  137. test_wait_timeout_any();
  138. std::cout << std::endl << std::endl;
  139. test_wait_timeout_all_invalid();
  140. std::cout << std::endl;
  141. test_wait_timeout_all();
  142. std::cout << std::endl;
  143. }