开发者

Encapsulate thread function with boost bind

开发者 https://www.devze.com 2023-04-13 03:26 出处:网络
I am currently creating boost::threads like this: boost::thread m_myThread; //member variable //... m_myThread = boost::thread(boost::bind(&MyClass::myThreadFunction, this));

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) [w开发者_如何学Pythonith 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消