我想做一些操作,比如每200秒检查一次。如何让io_service变量每200秒发布一次这个操作函数?在boost中有任何库函数吗?
答案 0 :(得分:0)
将来使用deadline_timer
到期时间为200秒,当async_wait
处理程序触发时,您可以调用您想要的任何功能
#include <boost/asio.hpp>
#include <iostream>
class Timer {
public:
explicit Timer(boost::asio::io_service& io_service) :
_timer( io_service )
{
this->wait();
}
private:
void wait()
{
_timer.expires_from_now(boost::posix_time::seconds(5));
_timer.async_wait([&](const boost::system::error_code& error) {
if (error) {
std::cerr << "Could not wait: " << error << std::endl;
return;
}
std::cout << "Done waiting!" << std::endl;
this->wait();
});
}
private:
boost::asio::deadline_timer _timer;
};
int
main()
{
boost::asio::io_service io_service;
Timer t{io_service};
io_service.run();
}