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