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