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

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