我的應用程序(C++,Windows)正在與外部設備進行通信。如果設備在一段時間後沒有應答,我想重置一個狀態變量。使用C++超時後重置變量
我最初的做法是
auto timer = boost::asio::deadline_timer(io_svc);
timer.expires_from_now(boost::posix_time::seconds(10));
timer.async_wait(boost::bind(&Class::CurrRequestTimeout, this, boost::asio::placeholders::error));
io_svc.poll();
和超時功能
void Class::CurrRequestTimeout(const boost::system::error_code & ec)
{
if (ec)
{
// this timeout was canceled
return;
}
ResetStatusVariable();
}
這應該是非阻塞的,這就是爲什麼我選擇的民意調查()而不是run()的(看到here) 。但是,使用poll()超時方法永遠不會被調用。使用run()它工作得很好,但是這會阻止執行。
應該調用超時方法的代碼在哪裏?如果你沒有粘貼它,請做。如果沒有這樣的代碼,那麼你的問題。 –
async_wait()應在超時過期或取消後調用超時方法。至少這是我的理解http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/reference/basic_deadline_timer/async_wait.html – Simon
如果這是正確的,它會怎麼做?沒有其他線索,至少不是你告訴我們的。 'async_wait'現在不能調用這個函數,因爲它還沒有結束。那麼這怎麼可能呢? –