基于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. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <thread>
  6. #include "librf.h"
  7. using namespace resumef;
  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. }