1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-02 00:00:11 +08:00
librf/tutorial/test_async_timer.cpp

36 lines
666 B
C++
Raw Normal View History

2020-03-26 17:26:39 +08:00
2017-09-24 14:01:30 +08:00
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include "librf.h"
using namespace resumef;
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)
{
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)
{
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
}