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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. future_t<> test_routine_use_timer()
  9. {
  10. using namespace std::chrono;
  11. void* frame_ptr = _coro_frame_ptr();
  12. size_t frame_size = _coro_frame_size();
  13. std::cout << "test_routine_use_timer" << std::endl;
  14. std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << promise_align_size<>() << std::endl;
  15. auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr);
  16. auto st = handler.promise()._state;
  17. scheduler_t* sch = st->get_scheduler();
  18. auto parent = st->get_parent();
  19. std::cout << "st=" << st.get() << ", scheduler=" << sch << ", parent=" << parent << std::endl;
  20. for (size_t i = 0; i < 3; ++i)
  21. {
  22. co_await resumef::sleep_for(100ms);
  23. std::cout << "timer after 100ms" << std::endl;
  24. std::cout << "1:frame=" << _coro_frame_ptr() << std::endl;
  25. }
  26. }
  27. future_t<> test_routine_use_timer_2()
  28. {
  29. std::cout << "test_routine_use_timer_2" << std::endl;
  30. co_await test_routine_use_timer();
  31. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  32. co_await test_routine_use_timer();
  33. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  34. co_await test_routine_use_timer();
  35. std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
  36. }
  37. void resumable_main_routine()
  38. {
  39. //go test_routine_use_timer_2();
  40. go test_routine_use_timer();
  41. this_scheduler()->run_until_notask();
  42. }