3

In boost::thread is it possible to call a class method with out making the class callable and implementing the void operator()() as in just call the class method

   for(int i=0;i<5;i++)
    boost::thread worker(myclass.myfunc,i,param2);

I get an error <unresolved overloaded function type>

Actually I would prefer to know the same for zi::thread

h1vpdata
  • 331
  • 2
  • 15

2 Answers2

3

For boost::thread you can use boost::bind to call a class member function.

myclass obj;
for(int i=0;i<5;i++)
        boost::thread worker(boost::bind(&myclass::myfunc,&obj,i,param2));
msh
  • 457
  • 1
  • 5
  • 10
3

boost::thread doesn't need anything special, it will work exactly as you want (minus syntax errors):

for (int i = 0; i != 5; ++i)
    boost::thread worker(&myclass::myfunc, myclassPointer, i, param2);

From the boost.thread docs:

template <class F,class A1,class A2,...>
thread(F f,A1 a1,A2 a2,...);

Effects: As if thread(boost::bind(f, a1, a2, ...)). Consequently, f and each aN are copied into internal storage for access by the new thread.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • I think this works only if the myclass::myfunc is static member function. You have to pass the object of the class to invoke the class member function. – msh Apr 15 '11 at 06:34
  • @msh : Right, exactly as you would with boost.bind. Not sure what you're getting at... – ildjarn Apr 15 '11 at 06:37
  • I try to say, you can't use as boost::thread worker(&myclass::myfunc, i, param2), if the myclass::myfunc is not static member function (and this won't compile). – msh Apr 15 '11 at 06:48