I am receiving an error of error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex'
I have seen various variations of questions regarding the same error but can't not figure out the solution yet. I am trying to implement a callback function in a thread. The callback function is being called through a member function as shown below:
// KinectGrabber.h
class KinectGrabber{
private:
mutable boost::mutex cloud_mutex_;
public:
KinectGrabber() { };
void run ();
void cloud_cb_ (const CloudPtr& cloud);
};
// KinectGrabber.cpp
void KinectGrabber::cloud_cb_ (const CloudPtr& cloud)
{
boost::mutex::scoped_lock lock (KinectGrabber::cloud_mutex_);
// capturing the point cloud
}
void KinectGrabber::run() {
// make callback function from member function
boost::function<void (const CloudPtr&)> f =
boost::bind (&KinectGrabber::cloud_cb_, this, _1);
// connect callback function for desired signal. In this case its a point cloud with color values
boost::signals2::connection c = interface->registerCallback (f);
}
int main (int argc, char** argv)
{
KinectGrabber kinect_grabber;
//kinect_grabber.run(); //this works
boost::thread t1(&KinectGrabber::run,kinect_grabber); // doesnt work
t1.interrupt;
t1.join;
}
I am using multithreading as there are other functions which I need to run along with this. Thank you for your assistance.
Found out the answer after some ordeal. The problem for this kind of error is the class cannot be copied (Ref: boost mutex strange error with private member). So the solution is this:
boost::thread t1(&KinectGrabber::run,boost::ref(kinect_grabber));
The boost::thread by default copies the object by value thereby violating the non-copyable criterion. Changing it to passing by reference in the thread solves the error. Hopefully it helps all others.