#include
#include
#include
boost::asio::io_service io;
//boost::asio::io_context io; //boost::asio::io_service 的升级
//此地方一定要给时间,否则定时器不起作用
boost::asio::deadline_timer timer_(io, boost::posix_time::seconds(0));
boost::asio::steady_timer timer1_(io,std::chrono::seconds(0));
boost::asio::deadline_timer timer2_(io);
boost::asio::steady_timer timer3_(io);
int g_begin = 0;
void start_time()
{
g_begin = std::chrono::duration_cast
//此地方expires_at()是从起始时间点开始算
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(5));
timer_.async_wait([](const boost::system::error_code& ec) {
if (ec) {
return;
}
auto end = std::chrono::duration_cast
std::cout << end - g_begin << std::endl;
end = g_begin;
start_time();
});
}
void start_time1()
{
g_begin = std::chrono::duration_cast
timer1_.expires_at(timer1_.expires_at() + std::chrono::seconds(5));
timer1_.async_wait([](const boost::system::error_code& ec) {
if (ec) {
return;
}
auto end = std::chrono::duration_cast
std::cout << end - g_begin << std::endl;
end = g_begin;
//定时器时间链
start_time1();
});
}
void start_time2()
{
g_begin = std::chrono::duration_cast
timer2_.expires_from_now(boost::posix_time::seconds(5));
timer2_.async_wait([](const boost::system::error_code& ec) {
if (ec) {
return;
}
auto end = std::chrono::duration_cast
std::cout << end - g_begin << std::endl;
end = g_begin;
//定时器时间链
start_time2();
});
}
void start_time3()
{
g_begin = std::chrono::duration_cast
timer3_.expires_from_now(std::chrono::seconds(5));
timer3_.async_wait([](const boost::system::error_code& ec) {
if (ec) {
return;
}
auto end = std::chrono::duration_cast
std::cout << end - g_begin << std::endl;
end = g_begin;
//定时器时间链
start_time3();
});
}
int main()
{
//start_time();
//start_time1();
//start_time2();
start_time3();
io.run();
}
总结:
expires_at 初始化要一个起始时间
expires_from_now 不给