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