基于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.2KB

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