基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_async_routine.cpp 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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_vt test_routine_use_timer()
  9. {
  10. using namespace std::chrono;
  11. std::cout << "test_routine_use_timer" << std::endl;
  12. for (size_t i = 0; i < 3; ++i)
  13. {
  14. co_await resumef::sleep_for(100ms);
  15. std::cout << "timer after 100ms" << std::endl;
  16. std::cout << "1:frame=" << _coro_frame_ptr() << ",promise=" << _coro_promise_ptr(void) << std::endl << std::endl;
  17. }
  18. }
  19. future_vt 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() << ",promise=" << _coro_promise_ptr(void) << std::endl << std::endl;
  24. co_await test_routine_use_timer();
  25. std::cout << "2:frame=" << _coro_frame_ptr() << ",promise=" << _coro_promise_ptr(void) << std::endl << std::endl;
  26. co_await test_routine_use_timer();
  27. std::cout << "2:frame=" << _coro_frame_ptr() << ",promise=" << _coro_promise_ptr(void) << std::endl << std::endl;
  28. }
  29. void resumable_main_routine()
  30. {
  31. go test_routine_use_timer_2();
  32. //go test_routine_use_timer();
  33. this_scheduler()->run_until_notask();
  34. }