首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::bind、boost::asio、boost::thread和类

boost::bind、boost::asio、boost::thread和类
EN

Stack Overflow用户
提问于 2009-08-26 19:17:41
回答 3查看 3.1K关注 0票数 1
代码语言:javascript
复制
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):error C2064: term不会计算为采用%1参数的函数

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-08-26 21:44:13

错误消息的末尾提供了解释:

代码语言:javascript
复制
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的线程,写下:

代码语言:javascript
复制
boost::thread thrd(boost::bind(&io_service::run, &io));

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

代码语言:javascript
复制
boost::thread thrd(&io_service::run, &io);
票数 4
EN

Stack Overflow用户

发布于 2009-08-26 19:59:44

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

代码语言:javascript
复制
t.async_wait(boost::bind(&sau_timer::exec, this, _1));

你有没有试过,还有其他的问题?

票数 0
EN

Stack Overflow用户

发布于 2009-08-26 20:28:19

我也需要将它与strnd.wrap()一起使用。我再次将其更改为:

代码语言:javascript
复制
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); }

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

代码语言:javascript
复制
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跳过==========

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1336895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档