我io_service在一个线程中运行了一个 boost ,我想在某个客户端发生某个事件后 6 秒在该线程中触发一个回调,如果该客户端已经在运行,则重置该客户端的计时器。
我unordered_map<string, shared_ptr<deadline_timer>>为每个客户维护一个计时器。
但是,在设置时async_wait,我的回调在分配的时间量(io_serviceIS 运行)后不会触发,当我重置指针(应该调用现有计时器的析构函数,导致它)时,它也不会触发(带有错误代码)发布到服务)。我怎样才能解决这个问题?
这是我的代码的相关部分:
auto it = timersByClientId.find(clientId);
if (it == timersByClientId.end())
{
onLogonChangeCallback(clientId, true);
timersByClientId[clientId].reset(
new boost::asio::deadline_timer(replyService, boost::posix_time::seconds(6))
);
it = timersByClientId.find(clientId);
}
else
{
// Cancel current wait operation (should fire the callback with an error code)
it->second.reset(
new boost::asio::deadline_timer(replyService, boost::posix_time::seconds(6))
);
}
it->second->async_wait([this, clientId](const boost::system::error_code& err) {
if (!err)
{
onLogonChangeCallback(clientId, false);
}
});
Run Code Online (Sandbox Code Playgroud)
如果它有任何改变,我将在 Visual C++ 2010 和 boost 1.47.0 下运行。