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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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();
  15. c_done << true;
  16. sch_in_thread->run();
  17. }
  18. template<class _Ctype>
  19. static void callback_get_long(int64_t val, _Ctype&& cb)
  20. {
  21. using namespace std::chrono;
  22. std::thread([val, cb = std::forward<_Ctype>(cb)]
  23. {
  24. std::this_thread::sleep_for(500ms);
  25. cb(val * val);
  26. }).detach();
  27. }
  28. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  29. static future_t<int64_t> async_get_long(int64_t val)
  30. {
  31. resumef::awaitable_t<int64_t> awaitable;
  32. callback_get_long(val, [awaitable](int64_t val)
  33. {
  34. awaitable.set_value(val);
  35. });
  36. return awaitable.get_future();
  37. }
  38. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  39. static future_t<> resumable_get_long(int64_t val, channel_t<bool> & c_done)
  40. {
  41. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  42. co_await via(sch_in_thread);
  43. val = co_await async_get_long(val);
  44. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  45. co_await *sch_in_main;
  46. val = co_await async_get_long(val);
  47. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  48. co_await *sch_in_thread;
  49. val = co_await async_get_long(val);
  50. std::cout << "thread = " << std::this_thread::get_id() << ", value = " << val << std::endl;
  51. c_done << true;
  52. }
  53. void resumable_main_switch_scheduler()
  54. {
  55. sch_in_main = this_scheduler();
  56. channel_t<bool> c_done{ 1 };
  57. std::cout << "main thread = " << std::this_thread::get_id() << std::endl;
  58. std::thread(&run_in_thread, std::ref(c_done)).detach();
  59. GO
  60. {
  61. co_await c_done;
  62. go resumable_get_long(3, c_done);
  63. co_await c_done;
  64. };
  65. sch_in_main->run_until_notask();
  66. }