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

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