【问题标题】:Boost-asio deadline_timer runtime errorBoost-asio deadline_timer 运行时错误
【发布时间】:2014-08-09 11:48:30
【问题描述】:

所以,我正在学习一个教程,尝试设置一个基本计时器...

void print(const boost::system::error_code &e)
{
  std::cout <<"hello world"<< std::endl;
}

int main()
{
  boost::asio::io_service io;
  boost::asio::deadline_timer timer(io, boost::posix_time::seconds(5));
  timer.async_wait(print);
  io.run();
}

构建良好,但在运行时;

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  thread: The attempted operation is not supported for the type of object referenced

Win7, GCC, 代码::blocks

编辑;在另一台具有相同设置的机器上进行了尝试 - 结果相同。

再次编辑;我应该从 boost 1_47 切换到最新版本吗?

【问题讨论】:

  • 你链接到 pthreads 了吗?为 GCC 提供-pthreads
  • 没有骰子。是否有我必须#include 的标题?目前我只有 asio.hpp 和 iostream。
  • 标题照原样很好。
  • 你现在推荐什么?
  • 我建议你等待认识你情况的人过来:|如果我知道答案,我想我会马上告诉你。耐心,学徒(哦,并且阅读

标签: c++ boost boost-asio


【解决方案1】:

您的代码适用于我使用 MinGw 4.8 和 boost 1.55。但是,请尝试将对 timer.async_wait 的调用更改为:

#include <boost/bind.hpp>

timer.async_wait(boost::bind(&print, boost::asio::placeholders::error));

【讨论】:

  • 提升 1.47 ?!!我绝对推荐使用 boost 1.55。 Note 1.56 今天发布了,可能有点太新了……
  • 好的,但是你认为这是我的问题的原因吗?
  • boost 1.48.0 changelog中,表示deadline_timer的实现发生了变化。反正新版本这么多,升级对你没有坏处。
【解决方案2】:

我切换了 boost 1.56,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多