-1

I am trying to capsulate a thread as it is shown here: http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html

But somehow I can't even compile my program.. This:

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


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

just gives me this:

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

This is the template-constructor that is used:

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

I really do not have any idea what the problem is..

EDIT:

Anyway, it works with this.. but this way I can't pass any parameter..

/**
 *
 */
void AcceptConnectionThread::start(int param)
{
  m_thread = boost::thread(&AcceptConnectionThread::AcceptConnectionThreadRun);
}
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • what if you change `AcceptConnectionThreadRun` declaration instead to take parameters? – didierc Jan 31 '13 at 16:25
  • @didierc The Tutorial says "The Worker::start() method spawns the thread which will run the processQueue method. Notice how we pass in this as the first bound parameter? Because we are using an instance method (and not a class method or regular function), we must ensure the first parameter is the instance pointer. The N parameter is the first actual parameter for the thread function, as can be seen in its signature." I am not sure how to work around this.. But it should work anyway. Any guess what could cause the problem? – Stefan Falk Jan 31 '13 at 16:30
  • an oversight. This bind operator needs the instance and the parameters to be passed to the method, but here there's an extra parameter `0` compared to the method which takes none. See [this SO question](http://stackoverflow.com/questions/527413/how-boostfunction-and-boostbind-work) for details on `boost::bind`. – didierc Jan 31 '13 at 16:51
  • btw, that `0` should perhaps be the `param` value which is passed to `AcceptConnectionThread::start`, but I did not try the tutorial, so take this with a grain of salt (unless you don't like salt). – didierc Jan 31 '13 at 16:55
  • I did not really come to a solution.. for now I am starting a thread without any parameters but sooner or later I will have to change this.. – Stefan Falk Jan 31 '13 at 19:20

1 Answers1

0

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

a. seo
  • 3
  • 4
chenjun
  • 301
  • 3
  • 10