我有一個要求,我的計時器必須根據2個條件進行重置,以先發生者爲準。如何重新啓動提升截止日期計時器
- 當計時器到期
- 當滿足特定條件(如內存達到某一限度)
我以下步驟:
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
boost::mutex mtx1;
void run_io_service()
{
io.run();
}
void print(const boost::system::error_code& /*e*/)
{
boost::mutex::scoped_lock lock(mtx1);
std::cout << "Hello, world!\n";
t.expires_from_now(boost::posix_time::seconds(1));
t.async_wait(print);
std::cout << "Print executed\n";
}
int main()
{
t.async_wait(print);
boost::thread monitoring_thread = boost::thread(run_io_service);
boost::this_thread::sleep(boost::posix_time::seconds(2));
t.cancel();
std::cout << "Resetting Timer\n";
t.async_wait(print);
boost::this_thread::sleep(boost::posix_time::seconds(2));
t.cancel();
io.stop();
monitoring_thread.join();
return 0;
}
此代碼工作正常,直到時間計時器尚未取消。 一旦定時器被取消,定時器不能以預期的方式工作,它根本不起作用。
我在做什麼錯?
你能否澄清你期望這個程序輸出什麼,它實際輸出什麼,以及爲什麼你認爲這意味着定時器不工作? – 2014-11-24 08:10:12