基于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_routine.cpp 1.3KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <thread>
  6. #include "librf/librf.h"
  7. using namespace librf;
  8. #ifndef __GNUC__ //GCC: 没有提供__builtin_coro_frame这样的内置函数
  9. future_t<> test_routine_use_timer()
  10. {
  11. using namespace std::chrono;
  12. for (size_t i = 0; i < 3; ++i)
  13. {
  14. co_await librf::sleep_for(100ms);
  15. std::cout << "timer after 100ms" << std::endl;
  16. std::cout << "1:frame=" << _coro_frame_ptr() << std::endl;
  17. }
  18. }
  19. future_t<> test_routine_use_timer_2()
  20. {
  21. std::cout << "test_routine_use_timer_2" << std::endl;
  22. co_await test_routine_use_timer();
  23. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  24. co_await test_routine_use_timer();
  25. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  26. co_await test_routine_use_timer();
  27. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  28. }
  29. #endif //#ifndef __GNUC__
  30. void resumable_main_routine()
  31. {
  32. std::cout << __FUNCTION__ << std::endl;
  33. //go test_routine_use_timer_2();
  34. #ifndef __GNUC__ //GCC: 没有提供__builtin_coro_frame这样的内置函数
  35. go test_routine_use_timer();
  36. #endif //#ifndef __GNUC__
  37. this_scheduler()->run_until_notask();
  38. }
  39. #if LIBRF_TUTORIAL_STAND_ALONE
  40. int main()
  41. {
  42. resumable_main_routine();
  43. return 0;
  44. }
  45. #endif