线程中的回调函数
Callback function in thread
我收到error C2248: 'boost::mutex::mutex' : cannot access private member declared in class '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;
}
我使用的是多线程,因为我需要同时运行其他函数。感谢您的帮助。
运行是一个非静态函数,您不需要按照现在的方式执行只需调用其中的"cloudcp"函数若运行是静态的,我会理解一些代码,但它不是,而且它正在使用这个指针!你可以坚持使用代码,只需保持运行功能的简单
并且您将需要在boost::线程处进行boost:检查这个使用boost线程和非静态类函数
经过一番磨难,终于找到了答案。这种错误的问题是无法复制类(Ref:boost mutex与私有成员发生奇怪错误)。因此,解决方案是:
boost::thread t1(&KinectGrabber::run,boost::ref(kinect_grabber));
boost::线程默认情况下按值复制对象,从而违反了不可复制的标准。将其更改为在线程中通过引用传递解决了错误。希望它能帮助所有其他人。
相关文章:
- 以特征类型作为参数的泛型函数回调
- C# DllImport 调用非托管C++函数回调
- 为什么模板参数推导失败,std::函数回调的可变参数模板参数?
- std::包含 std::函数回调的多个包装器的向量不起作用
- 如何设置对类的私有函数的函数回调?
- 如何使用 c++11 函数回调声明多个模板参数
- sqlite3 更改函数回调参数 void* not用于 int* C++
- 如何知道 sqlite 中的函数"回调"是否返回了一些东西?
- Windows表单如何使用其他函数回调图表信息
- 如何在 c++ 结构中的 void* 变量中调用存储函数(回调)
- 将成员函数作为标准函数回调
- C++ 使用成员函数回调实现按钮
- std::函数回调,参数采用观察者模式(寄存器主题上的占位符)
- 使用可变参数模板从 Lua 函数回调 (C api) 中提取参数
- 在C++中使用派生类的静态方法作为函数回调
- 将libuv与函数结构一起使用,而不是函数回调
- 如何将函数回调传递给类成员
- c++类成员函数回调
- C++函数回调:无法从成员函数转换为函数签名
- 如何编写一个通用函数回调测试类