I have this problem with boost::thread
that i cannot solve.
I have a classX.h
file:
#include <boost/thread/thread.hpp>
class classX{
...
void startWork(void);
void doWork(void);
...
}
and then a .cpp
file:
...
void classX::startWork(){
boost::thread(&doWork);
}
void classX::doWork(){
...
}
I cannot compile, i obtein the error (at the line in which i do boost::thread(&doWork)
):
error C2761: 'void plsa_mt_2::doWork(void)' : member function redeclaration not allowed
Is this error related with the thread creation or with something else? What can i do to solve it?
Since classX::doWork()
is a member function of classX
, you can't call the member function pointer (&classX::doWork)
without providing a pointer to a classX
.
The Boostiest way to accomplish this is by using Boost Bind to create a callable functor with the member function pointer and a pointer to the classX, like so:
void classX::startWork() {
boost::thread t(boost::bind(&classX::doWork, this)); // be careful, the boost::thread will be destroyed when this function returns
}
You could alternatively make doWork()
a static member function or a global function if doWork()
doesn't need access to instance properties of the classX
: