基于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 1.5KB

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