基于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_switch_scheduler.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static scheduler_t* sch_in_main = nullptr;
  9. static std::atomic<scheduler_t*> sch_in_thread = nullptr;
  10. void run_in_thread(channel_t<bool> c_done)
  11. {
  12. local_scheduler my_scheduler; //产生本线程唯一的调度器
  13. sch_in_thread = this_scheduler(); //本线程唯一的调度器赋值给sch_in_thread,以便于后续测试直接访问此线程的调度器
  14. (void)c_done.write(true); //数据都准备好了,通过channel通知其他协程可以启动后续依赖sch_in_thread变量的协程了
  15. //循环直到sch_in_thread为nullptr
  16. for (;;)
  17. {
  18. auto sch = sch_in_thread.load(std::memory_order_acquire);
  19. if (sch == nullptr)
  20. break;
  21. sch->run_one_batch();
  22. std::this_thread::yield();
  23. }
  24. }
  25. template<class _Ctype>
  26. static void callback_get_long_switch_scheduler(int64_t val, _Ctype&& cb)
  27. {
  28. using namespace std::chrono;
  29. std::thread([val, cb = std::forward<_Ctype>(cb)]
  30. {
  31. std::this_thread::sleep_for(500ms);
  32. cb(val + 1);
  33. }).detach();
  34. }
  35. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  36. static future_t<int64_t> async_get_long_switch_scheduler(int64_t val)
  37. {
  38. awaitable_t<int64_t> awaitable;
  39. callback_get_long_switch_scheduler(val, [awaitable](int64_t result)
  40. {
  41. awaitable.set_value(result);
  42. });
  43. return awaitable.get_future();
  44. }
  45. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  46. static future_t<> resumable_get_long_switch_scheduler(int64_t val, channel_t<bool> c_done)
  47. {
  48. std::cout << "thread = " << std::this_thread::get_id();
  49. std::cout << ", scheduler = " << current_scheduler();
  50. std::cout << ", value = " << val << std::endl;
  51. co_await *sch_in_thread;
  52. val = co_await async_get_long_switch_scheduler(val);
  53. std::cout << "thread = " << std::this_thread::get_id();
  54. std::cout << ", scheduler = " << current_scheduler();
  55. std::cout << ", value = " << val << std::endl;
  56. co_await *sch_in_main;
  57. val = co_await async_get_long_switch_scheduler(val);
  58. std::cout << "thread = " << std::this_thread::get_id();
  59. std::cout << ", scheduler = " << current_scheduler();
  60. std::cout << ", value = " << val << std::endl;
  61. co_await *sch_in_thread;
  62. val = co_await async_get_long_switch_scheduler(val);
  63. std::cout << "thread = " << std::this_thread::get_id();
  64. std::cout << ", scheduler = " << current_scheduler();
  65. std::cout << ", value = " << val << std::endl;
  66. co_await *sch_in_thread; //fake switch
  67. val = co_await async_get_long_switch_scheduler(val);
  68. std::cout << "thread = " << std::this_thread::get_id();
  69. std::cout << ", scheduler = " << current_scheduler();
  70. std::cout << ", value = " << val << std::endl;
  71. (void)c_done.write(true);
  72. }
  73. void resumable_main_switch_scheduler()
  74. {
  75. sch_in_main = this_scheduler();
  76. std::cout << "main thread = " << std::this_thread::get_id();
  77. std::cout << ", scheduler = " << sch_in_main << std::endl;
  78. channel_t<bool> c_done{ 1 };
  79. std::thread other(&run_in_thread, std::ref(c_done));
  80. go[&other, c_done]()->future_t<>
  81. {
  82. co_await c_done; //第一次等待,等待run_in_thread准备好了
  83. std::cout << "other thread = " << other.get_id();
  84. std::cout << ", sch_in_thread = " << sch_in_thread << std::endl;
  85. go resumable_get_long_switch_scheduler(1, c_done); //开启另外一个协程
  86. //co_await resumable_get_long(3, c_done);
  87. co_await c_done; //等待新的协程运行完毕,从而保证主线程的协程不会提早退出
  88. };
  89. sch_in_main->run_until_notask();
  90. //通知另外一个线程退出
  91. sch_in_thread.store(nullptr, std::memory_order_release);
  92. other.join();
  93. }