boost :: bind,boost :: asio,boost :: thread和classes

时间:2009-08-26 19:17:41

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

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'的构造函数中的代码(希望在一个单独的线程中运行一个计时器,然后调用另一个函数)。

不幸的是,当我尝试编译时,我收到以下错误:

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

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

3 个答案:

答案 0 :(得分:4)

解释在错误消息的末尾:

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时发生错误。它需要一个函数对象(带有opererator()()的东西),你传递它(我猜)是一个io :: service。如果你想要的是一个调用io_service :: run的线程,请写:

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

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

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

答案 1 :(得分:0)

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

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

您是否尝试过并遇到其他问题?

答案 2 :(得分:0)

我需要它与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跳过==========