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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. using namespace std::chrono;
  9. //非协程的逻辑线程,或异步代码,可以通过event_t通知到协程,并且不会阻塞协程所在的线程。
  10. static std::thread async_set_event_all(const event_v2::event_t & e, std::chrono::milliseconds dt)
  11. {
  12. return std::thread([=]
  13. {
  14. std::this_thread::sleep_for(dt);
  15. e.signal_all();
  16. });
  17. }
  18. static std::thread async_set_event_one(event_v2::event_t e, std::chrono::milliseconds dt)
  19. {
  20. return std::thread([=]
  21. {
  22. std::this_thread::sleep_for(dt);
  23. e.signal();
  24. });
  25. }
  26. static future_t<> resumable_wait_event(event_v2::event_t e, int idx)
  27. {
  28. if (co_await e)
  29. std::cout << "[" << idx << "]event signal!" << std::endl;
  30. else
  31. std::cout << "[" << idx << "]time out!" << std::endl;
  32. }
  33. static future_t<> resumable_wait_timeout(event_v2::event_t e, milliseconds dt, int idx)
  34. {
  35. if (co_await e.wait_for(dt))
  36. std::cout << "[" << idx << "]event signal!" << std::endl;
  37. else
  38. std::cout << "[" << idx << "]time out!" << std::endl;
  39. }
  40. static void test_notify_all()
  41. {
  42. event_v2::event_t evt;
  43. go resumable_wait_event(evt, 0);
  44. go resumable_wait_event(evt, 1);
  45. go resumable_wait_event(evt, 2);
  46. auto tt = async_set_event_all(evt, 100ms);
  47. this_scheduler()->run_until_notask();
  48. tt.join();
  49. }
  50. static void test_notify_one()
  51. {
  52. using namespace std::chrono;
  53. {
  54. event_v2::event_t evt;
  55. go resumable_wait_event(evt, 10);
  56. go resumable_wait_event(evt, 11);
  57. go resumable_wait_event(evt, 12);
  58. auto tt1 = async_set_event_one(evt, 100ms);
  59. auto tt2 = async_set_event_one(evt, 500ms);
  60. auto tt3 = async_set_event_one(evt, 800ms);
  61. this_scheduler()->run_until_notask();
  62. tt1.join();
  63. tt2.join();
  64. tt3.join();
  65. }
  66. }
  67. static void test_wait_all_timeout()
  68. {
  69. using namespace std::chrono;
  70. srand((int)time(nullptr));
  71. event_v2::event_t evts[10];
  72. std::vector<std::thread> vtt;
  73. for(size_t i = 0; i < _countof(evts); ++i)
  74. {
  75. go resumable_wait_timeout(evts[i], 100ms, (int)i);
  76. vtt.emplace_back(async_set_event_one(evts[i], 1ms * (50 + i * 10)));
  77. }
  78. this_scheduler()->run_until_notask();
  79. for (auto& tt : vtt)
  80. tt.join();
  81. }
  82. void resumable_main_event_v2()
  83. {
  84. test_notify_all();
  85. std::cout << std::endl;
  86. test_notify_one();
  87. std::cout << std::endl;
  88. test_wait_all_timeout();
  89. std::cout << std::endl;
  90. }