基于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_timer.cpp 771B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <thread>
  6. #include "librf/librf.h"
  7. using namespace librf;
  8. void resumable_main_timer()
  9. {
  10. using namespace std::chrono;
  11. auto th = this_scheduler()->timer()->add_handler(system_clock::now() + 5s,
  12. [](bool bValue)
  13. {
  14. if (bValue)
  15. std::cout << "timer canceled." << std::endl;
  16. else
  17. std::cout << "timer after 5s." << std::endl;
  18. });
  19. auto th2 = this_scheduler()->timer()->add_handler(1s,
  20. [&th](bool)
  21. {
  22. std::cout << "timer after 1s." << std::endl;
  23. th.stop();
  24. });
  25. this_scheduler()->run_until_notask();
  26. th2.stop(); //but th2 is invalid
  27. }
  28. #if LIBRF_TUTORIAL_STAND_ALONE
  29. int main()
  30. {
  31. resumable_main_timer();
  32. return 0;
  33. }
  34. #endif