基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test_async_routine.cpp 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #if LIBRF_TUTORIAL_STAND_ALONE
  39. int main()
  40. {
  41. resumable_main_routine();
  42. return 0;
  43. }
  44. #endif