用boost_bind封装线程函数

发布于 2024-12-09 16:16:38 字数 2769 浏览 0 评论 0原文

我目前正在创建这样的 boost::threads :

  boost::thread m_myThread; //member variable
  //...
  m_myThread = boost::thread(boost::bind(&MyClass::myThreadFunction, this));

这将启动一个执行成员函数“myThreadFunction”的线程。

我想封装一个新线程的启动,并使用类似

boost::thread m_myThread; //member variable
//...
startAThread(&m_myThread, boost::bind(&MyClass::myThreadFunction, this), 123 /*some int argument*/);

我认为可以实现“startAThread”的东西:

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread* threadToCreate, Callable func, int x)
{

    *threadToCreate = boost::thread(boost::bind(internal::threadhelper<Callable>, func, x));
}

但是,这无法编译,

usr/include/boost/bind/bind.hpp: In member function 'void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), A = boost::_bi::list0, A1 = boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, A2 = boost::_bi::value<int>]':
usr/include/boost/bind/bind_template.hpp:20:   instantiated from 'typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), L = boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> >]'
usr/include/boost/thread/detail/thread.hpp:61:   instantiated from 'void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> > >]'
MyClass.cpp:160:   instantiated from here
usr/include/boost/bind/bind.hpp:313: error: conversion from 'void' to non-scalar type 'boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >' requested

这个错误是什么意思?代码有什么问题吗?

多谢!

I am currently creating boost::threads like this:

  boost::thread m_myThread; //member variable
  //...
  m_myThread = boost::thread(boost::bind(&MyClass::myThreadFunction, this));

This will start a thread which executes a member function "myThreadFunction".

I would like to encapsulate the starting of a new thread and use something like

boost::thread m_myThread; //member variable
//...
startAThread(&m_myThread, boost::bind(&MyClass::myThreadFunction, this), 123 /*some int argument*/);

I thought I could implement "startAThread" like this:

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread* threadToCreate, Callable func, int x)
{

    *threadToCreate = boost::thread(boost::bind(internal::threadhelper<Callable>, func, x));
}

However, this fails to compile with

usr/include/boost/bind/bind.hpp: In member function 'void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), A = boost::_bi::list0, A1 = boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, A2 = boost::_bi::value<int>]':
usr/include/boost/bind/bind_template.hpp:20:   instantiated from 'typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), L = boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> >]'
usr/include/boost/thread/detail/thread.hpp:61:   instantiated from 'void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> > >]'
MyClass.cpp:160:   instantiated from here
usr/include/boost/bind/bind.hpp:313: error: conversion from 'void' to non-scalar type 'boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >' requested

What does this error mean? What's wrong with the code?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花开半夏魅人心 2024-12-16 16:16:38

尝试

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread& threadToCreate, Callable func, int x) {        
    threadToCreate = boost::thread(boost::bind(&internal::threadhelper<Callable>, func, x));
}

我也使用引用而不是指针。

Try

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread& threadToCreate, Callable func, int x) {        
    threadToCreate = boost::thread(boost::bind(&internal::threadhelper<Callable>, func, x));
}

I also used reference instead of pointer.

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