基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_async_yield_return.cpp 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. auto test_yield_int() -> generator_t<int>
  9. {
  10. std::cout << "1 will yield return" << std::endl;
  11. co_yield 1;
  12. std::cout << "2 will yield return" << std::endl;
  13. co_yield 2;
  14. std::cout << "3 will yield return" << std::endl;
  15. co_yield 3;
  16. std::cout << "4 will return" << std::endl;
  17. co_return 4;
  18. std::cout << "5 will never yield return" << std::endl;
  19. co_yield 5;
  20. }
  21. /*不能编译*/
  22. /*
  23. auto test_yield_void()
  24. {
  25. std::cout << "1 will yield return" << std::endl;
  26. co_yield ;
  27. std::cout << "2 will yield return" << std::endl;
  28. co_yield ;
  29. std::cout << "3 will yield return" << std::endl;
  30. co_yield ;
  31. std::cout << "4 will return" << std::endl;
  32. co_return ;
  33. std::cout << "5 will never yield return" << std::endl;
  34. co_yield ;
  35. }
  36. */
  37. auto test_yield_void() -> generator_t<>
  38. {
  39. std::cout << "block 1 will yield return" << std::endl;
  40. co_yield_void;
  41. std::cout << "block 2 will yield return" << std::endl;
  42. co_yield_void;
  43. std::cout << "block 3 will yield return" << std::endl;
  44. co_yield_void;
  45. std::cout << "block 4 will return" << std::endl;
  46. co_return_void;
  47. std::cout << "block 5 will never yield return" << std::endl;
  48. co_yield_void;
  49. }
  50. void resumable_main_yield_return()
  51. {
  52. go test_yield_int();
  53. this_scheduler()->run_until_notask();
  54. go test_yield_void();
  55. this_scheduler()->run_until_notask();
  56. }