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

123456789101112131415161718192021222324252627282930313233343536
  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. void resumable_main_timer()
  10. {
  11. using namespace std::chrono;
  12. auto th = this_scheduler()->timer()->add_handler(system_clock::now() + 5s,
  13. [](bool bValue)
  14. {
  15. if (bValue)
  16. std::cout << "timer canceled." << std::endl;
  17. else
  18. std::cout << "timer after 5s." << std::endl;
  19. });
  20. auto th2 = this_scheduler()->timer()->add_handler(1s,
  21. [&th](bool)
  22. {
  23. std::cout << "timer after 1s." << std::endl;
  24. th.stop();
  25. });
  26. this_scheduler()->run_until_notask();
  27. th2.stop(); //but th2 is invalid
  28. }