基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_async_switch_scheduler.cpp 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. std::cout << "other thread = " << std::this_thread::get_id() << std::endl;
  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(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 * val);
  34. }).detach();
  35. }
  36. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  37. static future_t<int64_t> async_get_long(int64_t val)
  38. {
  39. awaitable_t<int64_t> awaitable;
  40. callback_get_long(val, [awaitable](int64_t val)
  41. {
  42. awaitable.set_value(val);
  43. });
  44. return awaitable.get_future();
  45. }
  46. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  47. static future_t<> resumable_get_long(int64_t val, channel_t<bool> & c_done)
  48. {
  49. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  50. co_await *sch_in_thread;
  51. val = co_await async_get_long(val);
  52. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  53. co_await *sch_in_main;
  54. val = co_await async_get_long(val);
  55. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  56. co_await *sch_in_thread;
  57. val = co_await async_get_long(val);
  58. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  59. (void)c_done.write(true);
  60. }
  61. void resumable_main_switch_scheduler()
  62. {
  63. sch_in_main = this_scheduler();
  64. channel_t<bool> c_done{ 1 };
  65. std::cout << "main thread = " << std::this_thread::get_id() << std::endl;
  66. std::thread other(&run_in_thread, std::ref(c_done));
  67. GO
  68. {
  69. co_await c_done; //第一次等待,等待run_in_thread准备好了
  70. go resumable_get_long(3, c_done); //开启另外一个协程
  71. //co_await resumable_get_long(3, c_done);
  72. co_await c_done; //等待新的协程运行完毕,从而保证主线程的协程不会提早退出
  73. };
  74. sch_in_main->run_until_notask();
  75. //通知另外一个线程退出
  76. sch_in_thread.store(nullptr, std::memory_order_release);
  77. other.join();
  78. }