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

发布于 2024-08-03 14:17:58 字数 675 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殊姿 2024-08-10 14:17:58

错误消息的末尾有解释:

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);

The explanation is at the end of the error messages:

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

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:

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

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:

boost::thread thrd(&io_service::run, &io);
沉鱼一梦 2024-08-10 14:17:58

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

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

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

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:

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

Did you try it and had some other problems?

灰色世界里的红玫瑰 2024-08-10 14:17:58

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

I need it to be used with strnd.wrap() aswell. I've changed it to this again:

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); }

But now I get these errors:

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)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文