基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_async_yield_return.cpp 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. //std::experimental::generator<int>
  9. auto 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. 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()
  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. void resumable_main_yield_return()
  52. {
  53. go test_yield_int();
  54. this_scheduler()->run_until_notask();
  55. go test_yield_void();
  56. this_scheduler()->run_until_notask();
  57. }