Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost multiple calls to class method

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

like image 634
h1vpdata Avatar asked Feb 20 '26 07:02

h1vpdata


2 Answers

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.

like image 101
ildjarn Avatar answered Feb 22 '26 02:02

ildjarn


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));
like image 27
msh Avatar answered Feb 22 '26 02:02

msh