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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. template<class _Ctype>
  9. static void callback_get_long(int64_t val, _Ctype&& cb)
  10. {
  11. using namespace std::chrono;
  12. std::thread([val, cb = std::forward<_Ctype>(cb)]
  13. {
  14. std::this_thread::sleep_for(500ms);
  15. cb(val * val);
  16. }).detach();
  17. }
  18. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  19. static future_t<int64_t> async_get_long(int64_t val)
  20. {
  21. resumef::awaitable_t<int64_t> awaitable;
  22. callback_get_long(val, [awaitable](int64_t val)
  23. {
  24. awaitable.set_value(val);
  25. });
  26. return awaitable.get_future();
  27. }
  28. static future_t<int64_t> wait_get_long(int64_t val)
  29. {
  30. co_return co_await async_get_long(val);
  31. }
  32. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  33. static future_t<int64_t> resumable_get_long(int64_t val)
  34. {
  35. std::cout << val << std::endl;
  36. val = co_await wait_get_long(val);
  37. std::cout << val << std::endl;
  38. val = co_await wait_get_long(val);
  39. std::cout << val << std::endl;
  40. val = co_await wait_get_long(val);
  41. std::cout << val << std::endl;
  42. co_return val;
  43. }
  44. static future_t<int64_t> loop_get_long(int64_t val)
  45. {
  46. std::cout << val << std::endl;
  47. for (int i = 0; i < 5; ++i)
  48. {
  49. val = co_await async_get_long(val);
  50. std::cout << val << std::endl;
  51. }
  52. co_return val;
  53. }
  54. void resumable_main_cb()
  55. {
  56. //由于使用者可能不能明确的区分是resume function返回的awaitor还是awaitable function返回的awaitor
  57. //导致均有可能加入到协程里去调度。
  58. //所以,协程调度器应该需要能处理这种情况。
  59. go async_get_long(3);
  60. resumef::this_scheduler()->run_until_notask();
  61. GO
  62. {
  63. auto val = co_await resumable_get_long(2);
  64. std::cout << "GO:" << val << std::endl;
  65. };
  66. go loop_get_long(3);
  67. resumef::this_scheduler()->run_until_notask();
  68. }