【问题标题】:boost::bind, boost::asio, boost::thread, and classesboost::bind、boost::asio、boost::thread 和类
【发布时间】:2009-08-26 19:17:41
【问题描述】:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io), 
    t(io, boost::posix_time::seconds(secs))
{
    assert(secs > 0);
    this->f = f;

    //t.async_wait(boost::bind(&sau_timer::exec, this, _1));
    t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this)));
    boost::thread thrd(&io,this);
    io.run();
    //thrd(&sau_timer::start_timer);
}

这是我在类 'sau_timer' 的构造函数中的代码(它有望在单独的线程中运行一个计时器,然后调用另一个函数)。

不幸的是,atm 尝试编译时,出现以下错误:

1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : 错误 C2064: term 不计算为带 1 个参数的函数

以及一大堆警告。我究竟做错了什么?我已经尝试了我能想到的一切,谢谢。

【问题讨论】:

  • 你能给出 sau_timer::exec 声明吗?它是无 arg 成员函数、一个 arg 静态函数还是其他什么?
  • void sau_timer::exec(const boost::system::error_code&) { (f)(params); }

标签: c++ multithreading boost bind boost-asio


【解决方案1】:

解释在错误信息的最后:

c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) :
  see reference to function template instantiation 
  'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled

生成 boost::thread 的 ctor 时发生错误。它需要一个函数对象(带有 operator()() 的东西),并且你传递给它的东西(我猜)是一个 io::service。如果你想要的是一个调用 io_service::run 的线程,写:

boost::thread thrd(boost::bind(&io_service::run, &io));

如果您使用的是相对较新的 Boost 版本,我相信线程的 ctor 有一个方便的重载来处理 bind(),允许简单地编写:

boost::thread thrd(&io_service::run, &io);

【讨论】:

    【解决方案2】:

    每个非静态成员函数都有一个第一个隐藏参数 - 要调用函数的实例。所以你的 exec 函数需要两个参数。你有适当的代码,但它被注释掉了。我的意思是:

    t.async_wait(boost::bind(&sau_timer::exec, this, _1));
    

    您尝试过之后遇到了其他问题吗?

    【讨论】:

      【解决方案3】:

      我也需要它与 strnd.wrap() 一起使用。我又改成了这个:

      sau_timer::sau_timer(int secs, timerparam f) : strnd(io), 
          t(io, boost::posix_time::seconds(secs))
      {
          assert(secs > 0);
          this->f = f;
      
          t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
          boost::thread thrd(&io);
          io.run();
      }
      void sau_timer::exec(const boost::system::error_code&) { (f)(params); }
      

      但现在我得到了这些错误:

      1>------ Build started: Project: Sauria, Configuration: Debug Win32 ------
      1>Compiling...
      1>sau_timer.cpp
      1>Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
      1>- add -D_WIN32_WINNT=0x0501 to the compiler command line; or
      1>- add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
      1>Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
      1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
      1>        c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<F,boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
      1>        with
      1>        [
      1>            A1=boost::_bi::value<sau_timer *>,
      1>            F=boost::asio::io_service *,
      1>            T=void,
      1>            A=boost::_bi::list0
      1>        ]
      1>        c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
      1>        with
      1>        [
      1>            R=void,
      1>            F=boost::asio::io_service *,
      1>            L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
      1>        ]
      1>        c:\program files\boost\boost_1_39\boost\thread\detail\thread.hpp(227) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
      1>        with
      1>        [
      1>            R=void,
      1>            F=boost::asio::io_service *,
      1>            L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
      1>        ]
      1>        c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) : see reference to function template instantiation 'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
      1>        with
      1>        [
      1>            F=boost::asio::io_service *,
      1>            A1=sau_timer *
      1>        ]
      1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm"
      1>Sauria - 1 error(s), 0 warning(s)
      

      ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        • 2013-10-05
        • 1970-01-01
        • 2011-07-11
        • 2019-10-16
        • 1970-01-01
        相关资源
        最近更新 更多