boost::bind、boost::asio、boost::thread 和类
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 does notvaluation to a function take 1争论
以及一大堆警告。我做错了什么?我已经尝试了我能想到的一切,谢谢。
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);
}
This is the code I have in the constructor for the class 'sau_timer' (which will hopefully run a timer in a seperate thread and then call another function).
Unfortunately, atm when I try to compile, I get the following error:
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
Aswell as a whole bunch of warnings. What am I doing wrong? I've tried everything I can think of, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误消息的末尾有解释:
生成 boost::thread 的 ctor 时发生错误。它需要一个函数对象(带有operator()()的东西),然后你传递给它(我猜)是一个io::service。如果你想要的是一个调用 io_service::run 的线程,请写:
如果你使用相对较新版本的 Boost,我相信线程的 ctor 有一个方便的重载来处理 bind(),允许简单地写:
The explanation is at the end of the error messages:
The error occurs while generating the ctor of boost::thread. It expects a function object (something with an opererator()()), and you pass it what (I guess) is an io::service. If what you want is a thread calling io_service::run, write:
If you use a relatively recent version of Boost, I believe that thread's ctor has a convenience overload that takes care of the bind(), allowing to simply write:
每个非静态成员函数都有一个第一个隐藏参数 - 要调用函数的实例。所以你的 exec 函数需要两个参数。你有适当的代码,但它被注释掉了。我的意思是:
您尝试过并遇到其他问题吗?
Each non-static member function has a first, hidden parameter - the instance on which function is to be called. So your exec function needs two arguments. And you have appropriate code, but it is commented out. I mean:
Did you try it and had some other problems?
我还需要它与 strnd.wrap() 一起使用。我再次将其更改为:
但现在我收到这些错误:
========== 构建:0 成功,1 失败,0 最新,0 跳过 ======= ===
I need it to be used with strnd.wrap() aswell. I've changed it to this again:
But now I get these errors:
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========