Creating a new asio timer instead of reusing the old one. Reusing leads to strange crashes. Whoever invented the semantics of the asio timer obviously did not consider the need to restart a timer within the handler. Oh well. The asio timer service also seems to sleep all the time (not using up 100% load if timeout of all timeouts are 0). Pending further tests, asio timers might be unsuitable.

This commit is contained in:
lotodore
2009-06-08 21:53:14 +00:00
parent c8594e8e7b
commit e53c60c263
2 changed files with 9 additions and 6 deletions
+8 -5
View File
@@ -42,7 +42,7 @@ TimerManager::RegisterTimer(unsigned timeoutMsec, boost::function<void()> timerH
data->durationMsec = timeoutMsec; data->durationMsec = timeoutMsec;
data->autoRestart = autoRestart; data->autoRestart = autoRestart;
data->cancelled = false; data->cancelled = false;
data->timer->async_wait(boost::bind(&TimerManager::Handler, boost::asio::placeholders::error, data)); data->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, data));
m_timerMap.insert(TimerMap::value_type(id, data)); m_timerMap.insert(TimerMap::value_type(id, data));
return id; return id;
@@ -57,8 +57,9 @@ TimerManager::AddTimer(unsigned timerId, unsigned timeoutMsec, boost::function<v
if (pos != m_timerMap.end()) if (pos != m_timerMap.end())
{ {
pos->second->userHandler = timerHandler; pos->second->userHandler = timerHandler;
pos->second->timer->expires_from_now(boost::posix_time::milliseconds(timeoutMsec)); pos->second->timer.reset(
pos->second->timer->async_wait(boost::bind(&TimerManager::Handler, boost::asio::placeholders::error, pos->second)); new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(timeoutMsec)));
pos->second->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, pos->second));
restarted = true; restarted = true;
} }
return restarted; return restarted;
@@ -83,13 +84,15 @@ TimerManager::UnregisterTimer(unsigned timerId)
void void
TimerManager::Handler(const boost::system::error_code &ec, boost::shared_ptr<TimerManager::TimerData> data) TimerManager::Handler(const boost::system::error_code &ec, boost::shared_ptr<TimerManager::TimerData> data)
{ {
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
if (!ec && data && !data->cancelled) if (!ec && data && !data->cancelled)
{ {
data->userHandler(); data->userHandler();
if (data->autoRestart) if (data->autoRestart)
{ {
data->timer->expires_from_now(boost::posix_time::milliseconds(data->durationMsec)); data->timer.reset(
data->timer->async_wait(boost::bind(&TimerManager::Handler, boost::asio::placeholders::error, data)); new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(data->durationMsec)));
data->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, data));
} }
} }
} }
+1 -1
View File
@@ -46,7 +46,7 @@ protected:
bool cancelled; bool cancelled;
}; };
static void Handler(const boost::system::error_code &ec, boost::shared_ptr<TimerData> data); void Handler(const boost::system::error_code &ec, boost::shared_ptr<TimerData> data);
typedef std::map<unsigned, boost::shared_ptr<TimerData> > TimerMap; typedef std::map<unsigned, boost::shared_ptr<TimerData> > TimerMap;
unsigned GetNextTimerId(); unsigned GetNextTimerId();