2021-11-01 17:59:08 +08:00
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
#include "librf/librf.h"
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
using namespace librf;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
void resumable_main_timer()
|
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
auto th = this_scheduler()->timer()->add_handler(system_clock::now() + 5s,
|
2017-09-24 14:01:30 +08:00
|
|
|
|
[](bool bValue)
|
2018-08-26 19:06:07 +08:00
|
|
|
|
{
|
|
|
|
|
if (bValue)
|
|
|
|
|
std::cout << "timer canceled." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "timer after 5s." << std::endl;
|
|
|
|
|
});
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
auto th2 = this_scheduler()->timer()->add_handler(1s,
|
2017-09-24 14:01:30 +08:00
|
|
|
|
[&th](bool)
|
2018-08-26 19:06:07 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "timer after 1s." << std::endl;
|
|
|
|
|
th.stop();
|
|
|
|
|
});
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
th2.stop(); //but th2 is invalid
|
|
|
|
|
}
|
2020-09-23 22:56:51 +08:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
resumable_main_timer();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|