【问题标题】:boost::asio::high_resolution_timer usage example?boost::asio::high_resolution_timer 使用示例?
【发布时间】:2015-10-27 10:25:40
【问题描述】:

我正在尝试使用 boost 的高分辨率计时器并出现编译错误。请在下面找到sn-p,

boost::asio::io_service             _svc;
boost::asio::high_resolution_timer  _timer(_svc, boost::chrono::seconds(1));

错误:

错误:没有匹配函数调用'boost::asio::basic_waitable_timer::basic_waitable_timer(boost::asio::io_service&, boost::chrono::seconds)'

【问题讨论】:

  • 使用 boost::posix_time::seconds(1) 代替 boost::chrono::seconds(1)。
  • 试过了,我得到了这个,错误:没有匹配函数调用'boost::asio::basic_waitable_timer<:chrono::_v2::system_clock>::basic_waitable_timer(boost: :asio::io_service&, boost::posix_time::seconds)'
  • @Jonathan 我对此表示赞同,但我们记错了(我认为那是为截止时间计时器)

标签: c++ boost timer boost-asio


【解决方案1】:

高分辨率计时器显然使用 C++11 的 chrono 库(如果可用):boost/asio/high_resolution_timer.hpp 第 48 行

#elif defined(BOOST_ASIO_HAS_STD_CHRONO)
typedef basic_waitable_timer<
    std::chrono::high_resolution_clock>
  high_resolution_timer;

Live On Coliru

#include <boost/asio.hpp>
#include <boost/asio/high_resolution_timer.hpp>

int main() {
    boost::asio::io_service             _svc;
    boost::asio::high_resolution_timer  _timer(_svc);

    _timer.expires_from_now(std::chrono::milliseconds(100));
}

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 2012-10-26
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多