boost::deadline_timer

  1. 通过cancel可以使waithandler立刻返回。所以必须在waithandler中检查errorcode,如果errorcode为0,那么执行需要的定时操作,如果errorcode不等于0,那么检查其是否等于boost::asio::error::operation_aborted,如果等于,那么表示timer是被cancel。
  2. deadline_timer析构的开销很小。
example:
waithandler:
【boost::deadline_timer】void checkZipVector( server_connection_type& conn, const boost::system::error_code& error ) { if(!error) { LOG4CXX_INFO(m_logger, boost::format("[AsyncTimer] Begin zip transaction: %1%") % m_zipCurVecLen); asyncWriteZipData(conn); m_timer->expires_from_now(boost::posix_time::seconds(m_timeout)); m_timer->async_wait(boost::bind(&server_work::checkZipVector, shared_from_this(), boost::ref(conn), boost::asio::placeholders::error)); } else if(error == boost::asio::error::operation_aborted) { LOG4CXX_INFO(m_logger, "[AsyncTimer] Cancel"); } }

    推荐阅读