基于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 731B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. int main()
  28. {
  29. resumable_main_timer();
  30. return 0;
  31. }