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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include <thread>
  7. #include "librf.h"
  8. using namespace resumef;
  9. generator_t<int> test_yield_int()
  10. {
  11. std::cout << "1 will yield return" << std::endl;
  12. co_yield 1;
  13. std::cout << "2 will yield return" << std::endl;
  14. co_yield 2;
  15. std::cout << "3 will yield return" << std::endl;
  16. co_yield 3;
  17. std::cout << "4 will return" << std::endl;
  18. co_return 4;
  19. std::cout << "5 will never yield return" << std::endl;
  20. co_yield 5;
  21. }
  22. /*不能编译*/
  23. /*
  24. auto test_yield_void()
  25. {
  26. std::cout << "1 will yield return" << std::endl;
  27. co_yield ;
  28. std::cout << "2 will yield return" << std::endl;
  29. co_yield ;
  30. std::cout << "3 will yield return" << std::endl;
  31. co_yield ;
  32. std::cout << "4 will return" << std::endl;
  33. co_return ;
  34. std::cout << "5 will never yield return" << std::endl;
  35. co_yield ;
  36. }
  37. */
  38. auto test_yield_void() -> generator_t<>
  39. {
  40. std::cout << "block 1 will yield return" << std::endl;
  41. co_yield_void;
  42. std::cout << "block 2 will yield return" << std::endl;
  43. co_yield_void;
  44. std::cout << "block 3 will yield return" << std::endl;
  45. co_yield_void;
  46. std::cout << "block 4 will return" << std::endl;
  47. co_return_void;
  48. std::cout << "block 5 will never yield return" << std::endl;
  49. co_yield_void;
  50. }
  51. auto test_yield_future() -> future_t<int64_t>
  52. {
  53. std::cout << "future 1 will yield return" << std::endl;
  54. co_yield 1;
  55. std::cout << "future 2 will yield return" << std::endl;
  56. co_yield 2;
  57. std::cout << "future 3 will yield return" << std::endl;
  58. co_yield 3;
  59. std::cout << "future 4 will return" << std::endl;
  60. co_return 4;
  61. std::cout << "future 5 will never yield return" << std::endl;
  62. co_yield 5;
  63. }
  64. void resumable_main_yield_return()
  65. {
  66. for (int i : test_yield_int())
  67. {
  68. std::cout << i << " had return" << std::endl;
  69. }
  70. go test_yield_int();
  71. this_scheduler()->run_until_notask();
  72. go test_yield_void();
  73. this_scheduler()->run_until_notask();
  74. go test_yield_future();
  75. this_scheduler()->run_until_notask();
  76. }