Boost-asio deadline_timer运行时错误

Boost-asio deadline_timer runtime error

本文关键字:运行时错误 timer deadline Boost-asio      更新时间:2023-10-16

所以,我正在遵循教程,试图设置一个基本的计时器…

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, code::blocks

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

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

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

#include <boost/bind.hpp>
timer.async_wait(boost::bind(&print, boost::asio::placeholders::error));

我切换了boost 1.56,它工作了