Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 线程中的回调函数_C++_Multithreading_Callback_Boost Thread - Fatal编程技术网

C++ 线程中的回调函数

C++ 线程中的回调函数,c++,multithreading,callback,boost-thread,C++,Multithreading,Callback,Boost Thread,我收到一个错误,错误为错误C2248:'boost::mutex::mutex':无法访问在类'boost::mutex'中声明的私有成员。 我已经看到了关于同一个错误的各种各样的问题,但还没有找到解决办法。我试图在线程中实现回调函数。通过成员函数调用回调函数,如下所示: // KinectGrabber.h class KinectGrabber{ private: mutable boost::mutex cloud_mutex_; public: KinectGra

我收到一个错误,错误为
错误C2248:'boost::mutex::mutex':无法访问在类'boost::mutex'中声明的私有成员。

我已经看到了关于同一个错误的各种各样的问题,但还没有找到解决办法。我试图在线程中实现回调函数。通过成员函数调用回调函数,如下所示:

// 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;
}
//KinectGrabber.h
类动态抓取器{
私人:
可变boost::互斥云;
公众:
KinectGrabber(){};
无效运行();
空云(const CloudPtr&cloud);
};
//KinectGrabber.cpp
void KinectGrabber::cloud\u cb\u(const CloudPtr&cloud)
{
boost::mutex::作用域锁定锁(KinectGrabber::cloud\u mutex);
//捕捉点云
}
void KinectGrabber::run(){
//从成员函数生成回调函数
boost::函数f=
boost::bind(&KinectGrabber::cloud\u cb\u,this,\u 1);
//连接所需信号的回调函数。在这种情况下,它是一个带有颜色值的点云
boost::signals2::connection c=接口->寄存器回调(f);
}
int main(int argc,字符**argv)
{
kinect抓取器kinect_抓取器;
//kinect_grabber.run();//这很有效
boost::线程t1(&KinectGrabber::run,kinect_grabber);//不工作
t1.中断;
t1.加入;
}

我使用多线程,因为我还需要运行其他函数。感谢您的帮助。

跑步是一项非静态功能,您不需要按照现在的方式进行 只需调用其中的“cloud\u cp”函数 若运行是静态的,我会理解一些代码,但它不是静态的,并且它使用的是this指针! 您可以继续使用代码,只需保持run函数简单即可

您将需要在boost::线程处执行boost::bind 检查这个

经过一番磨难,终于找到了答案。此类错误的问题是无法复制类(Ref:)。因此,解决方案是:

boost::thread t1(&KinectGrabber::run,boost::ref(kinect_grabber));

默认情况下,boost::thread按值复制对象,从而违反了不可复制的标准。将其更改为在线程中通过引用传递可以解决此错误。希望它能帮助所有其他人

你能再解释一下吗?我想我不太明白。