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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include "librf/librf.h"
  6. static const int M = 10;
  7. size_t dynamic_go_count = 0;
  8. std::array<std::array<std::array<int32_t, M>, M>, 3> dynamic_cells;
  9. void test_dynamic_go()
  10. {
  11. auto co_star = [](int j) -> librf::future_t<int>
  12. {
  13. for (int i = 0; i < M; ++i)
  14. {
  15. go[=]() -> librf::generator_t<int>
  16. {
  17. for (int k = 0; k < M; ++k)
  18. {
  19. ++dynamic_cells[j][i][k];
  20. ++dynamic_go_count;
  21. std::cout << j << " " << i << " " << k << std::endl;
  22. co_yield k;
  23. }
  24. co_return M;
  25. };
  26. co_yield i;
  27. }
  28. co_return M;
  29. };
  30. go co_star(0);
  31. go co_star(1);
  32. go co_star(2);
  33. librf::this_scheduler()->run_until_notask();
  34. std::cout << "dynamic_go_count = " << dynamic_go_count << std::endl;
  35. for (auto & j : dynamic_cells)
  36. {
  37. for (auto & i : j)
  38. {
  39. for (auto k : i)
  40. std::cout << k;
  41. std::cout << std::endl;
  42. }
  43. std::cout << std::endl;
  44. }
  45. }
  46. void resumable_main_dynamic_go()
  47. {
  48. test_dynamic_go();
  49. }
  50. #if LIBRF_TUTORIAL_STAND_ALONE
  51. int main()
  52. {
  53. resumable_main_dynamic_go();
  54. return 0;
  55. }
  56. #endif