-1

我正在尝试封装一个线程,如下所示:http: //antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html

但不知何故,我什至无法编译我的程序.. 这个:

/**
 *
 */
void AcceptConnectionThread::start(int param)
{
  m_thread = boost::thread(&AcceptConnectionThread::AcceptConnectionThreadRun, this, 0);
}


/**
 *
 */
int AcceptConnectionThread::AcceptConnectionThreadRun()
{
  return 0;
}

只是给我这个:

Description Resource    Path    Location    Type   required from `boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void; F = int (*)(); L = boost::_bi::list2<boost::_bi::value<AcceptConnectionThread*>, boost::_bi::value<int> >; boost::_bi::bind_t<R, F, L>::result_type = void]’   ServerNetwork       line 20, external location: /usr/include/boost/bind/bind_template.hpp   C/C++ Problem
Description Resource    Path    Location    Type   required from ‘void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, int (*)(), boost::_bi::list2<boost::_bi::value<AcceptConnectionThread*>, boost::_bi::value<int> > >]’    ServerNetwork       line 62, external location: /usr/include/boost/thread/detail/thread.hpp C/C++ Problem
Description Resource    Path    Location    Type too many arguments to function ServerNetwork       line 313, external location: /usr/include/boost/bind/bind.hpp   C/C++ Problem

这是使用的模板构造函数:

template <class F,class A1,class A2>
thread(F f,A1 a1,A2 a2):
    thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2)))
{
    start_thread();
}

我真的不知道问题是什么..

编辑:

无论如何,它适用于此..但是这样我不能传递任何参数..

/**
 *
 */
void AcceptConnectionThread::start(int param)
{
  m_thread = boost::thread(&AcceptConnectionThread::AcceptConnectionThreadRun);
}
4

1 回答 1

0

使用boost::bind



    /**
     *
     */
    void AcceptConnectionThread::start(int param)
    {
      int para_to_pass = 0;
      m_thread = boost::thread( boost::bind(&AcceptConnectionThread::AcceptConnectionThreadRun, this, para_to_pass));
    }


    /**
     *
     */
    int AcceptConnectionThread::AcceptConnectionThreadRun(int para)
    {
      // use para
      return 0;
    }

于 2013-02-01T08:47:20.700 回答