问题描述:

  • 使用多线程进行点云的实时重建, 在主线程中创建 PCLVisualizer 对象指针(即使智能指针boost::shared_ptr),传入子线程中进行显示(viewer->spinOnce(100),)结果crash
//主线程中:typedef boost::shared_ptr<pcl::visualization::PCLVisualizer> PViewer;//定义 PCLVisualizerPViewer pViewer (new pcl::visualization::PCLVisualizer ("ReconstructionViewer"));pViewer->initCameraParameters();pViewer->setCameraPosition(0, -15, -15,0,  0,  5,0, -1,  0);pViewer->addCoordinateSystem (0.5, 0, 0, 0);// 声明一个类Reconstruction,使用PCLVisualizer, // 将 PCLVisualizer对象pViewer 传入Reconstruction构造函数,赋值给其成员对象 mpViewermpReconstructor = new Reconstruction(pCloud, pViewer, pSock);//启动子线程线程 Run, 在Run中使用 mpViewer(即在主线程中定义的PCLVisualizer对象pViewer)mptReconstruction = new thread(&Reconstruction::Run,mpReconstructor);//子线程:void Reconstruction::Run(){//...//结果调用下面两个函数时 crashmpViewer->setCameraPosition(....);mpViewer->spinOnce(100);//...}

原因分析:

哪个个线程创建 PCLVisualizer 对象,那个线程有其使用权,传递给其他线程则不能使用。 一定要看库的说明

此类不能跨多个线程使用。 仅从创建它们的同一线程中调用此类对象的函数! 一些方法,例如 addPointCloud,如果从其他线程调用会崩溃。

namespace pcl
{template <typename T> class PointCloud;template <typename T> class PlanarPolygon;namespace visualization{/** \brief PCL Visualizer main class.* \author Radu B. Rusu* \ingroup visualization* \note This class can NOT be used across multiple threads. Only call functions of objects of this class* from the same thread that they were created in! Some methods, e.g. addPointCloud, will crash if called* from other threads.*///此类不能跨多个线程使用。 仅从创建它们的同一线程中调用此类对象的函数! 一些方法,例如 addPointCloud,如果从其他线程调用会崩溃。class PCL_EXPORTS PCLVisualizer{//...}}
}

其他参考:

  • 使用“ pcl ::可视化”来自一个类的不同实例的不同线程中(using &quot;pcl::visualization&quot; in different threads from different instance of a class)
I want to have a class which contain a visualizer on a cloud point. here is my code:class my_vis
{void vis_func (){pcl::visualization::PCLVisualizer *vis ;vis = new pcl::visualization::PCLVisualizer("octree viewer");// this "vis" is just used in this function and no other place}void execute(){//Start visualizer in a threadboost::thread* visThread = new boost::thread(boost::bind(&my_vis::vis_func, this));// bla bla}
}
int main ()
{    my_vis vis1();vis1.execute();my_vis vis2();vis2.execute();std::getchar();return 0 ;
}
now I have a class of visualizers which can be instantiated in "main". when I made just one instance from the class "my_vis" every thing is OK when the program runs. But I need two or more instances. and when I initialize more than one instance, an error occured: BLOCK_TYPE_IS_VALID I think that it is because of using threads. But threading is necessary in my project.Would you please help me? Thanks a lot for your patient and help :)P.S. I am using PCL 1.7
After two days, I finally solve this.I pay attention to the constructor of pcl::visualization::PCLVisualizer and also "SpinOnce" function and I recognize that if you put a static lock, so that just one thread among multiple objects can access these functions, then the problem will be solved.previously, I put non static locks on these function, and as you know local locks just work in the same object which they are created in (Not the whole objects which are instantiated from a class). So I defined a static lock in my_vis class:private static boost::mutex vis_mutex; boost::mutex my_vis::vis_mutex; //storage for static lock
and replace "vis->spinOnce(1)" with{ boost::mutex::scoped_lock vis_lock(vis_mutex); vis->spinOnce (); }
I still think that it is not a permanent solution, and this bug is better to be solved by pcl developers :)

感言:

  • 写bug,20%的时间就写完了,找bug,需要80%的时间;
  • 当你找bug找到自己快要crash时,答案就快要出现了。

PCL_PCLVisualizer在多线程中的使用问题(viewer spinOnce crash)相关推荐

  1. 【Linux】多线程中使用fork()

    (最核心的东西我在下面用红色字体标出来了,理解了那块,这些东西都是就理解了!) 在本篇文章开始之前,需要大家先了解线程和进程,这位大哥讲的言简意赅:进程和线程的主要区别(总结)_kuangsongha ...

  2. java闭合数据_java多线程中线程封闭详解

    线程封闭的概念 访问共享变量时,通常要使用同步,所以避免使用同步的方法就是减少共享数据的使用,这种技术就是线程封闭. 实现线程封闭的方法 1:ad-hoc线程封闭 这是完全靠实现者控制的线程封闭,他的 ...

  3. java多线程中的join方法详解

    java多线程中的join方法详解 方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. 自从接触Java多线程,一直对Join理解不了.JDK是这样说的:join p ...

  4. 了解多线程中的yield

    2019独角兽企业重金招聘Python工程师标准>>> 最近在学习多线程这一块,发现里面有好多让人产生误区的地方,今天我来分析下java多线程中的yield功能,希望其他朋友也可以从 ...

  5. 解决DataGridView在多线程中无法显示滚动条的问题

    解决DataGridView在多线程中无法显示滚动条的问题 参考文章: (1)解决DataGridView在多线程中无法显示滚动条的问题 (2)https://www.cnblogs.com/roph ...

  6. VMware 虚拟化编程(8) — 多线程中的 VixDiskLib

    目录 目录 前文列表 多线程注意事项 多线程中的 VixDiskLib 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化 ...

  7. 如何在多线程中调用winform窗体控件2——实例篇

    如何在多线程中调用winform窗体控件2--实例篇 针对之前文章<如何在多线程中调用winform窗体控件>,下面举个我项目中的实际案例,这是一个我自定义控件在异步设置焦点时的代码.在新 ...

  8. android串口补位,Rust多线程中的消息传递机制

    代码说话. use std::thread; use std::sync::mpsc; use std::time::Duration; fn main() { let (tx, rx) = mpsc ...

  9. JAVA多线程中join()方法的详细分析

    虽然关于讨论线程join()方法的博客已经非常极其特别多了,但是前几天我有一个困惑却没有能够得到详细解释,就是当系统中正在运行多个线程时,join()到底是暂停了哪些线程,大部分博客给的例子看起来都像 ...

  10. java 锁竞争_Java多线程中的竞争条件、锁以及同步的概念

    竞争条件 1.竞争条件: 在java多线程中,当两个或以上的线程对同一个数据进行操作的时候,可能会产生"竞争条件"的现象.这种现象产生的根本原因是因为多个线程在对同一个数据进行操作 ...

最新文章

  1. CKfinder2.0.2版本破解
  2. 个性化服务谋定移动电子商务-李玉庭:经信研究重整购物
  3. xml web service
  4. python中的return和print的区别_python中return和print的区别(详细)
  5. (6)Linux进程调度-实时调度器
  6. hive 列表去重_Hive企业级调优
  7. natapp 使用教程
  8. gnu/stubs-32.h
  9. 手把手教你注册和备案域名
  10. vue封装了个日历组件(包含农历,节日)
  11. 数据驱动业务,说的好听,做好很难!得这样才行
  12. 天地图实现标注用户当前坐标位置
  13. vim全局搜索当前目录
  14. 余文乐结婚,杜蕾斯文案炸了!
  15. Golang 二叉树遍历
  16. 点击放大 swiper+photoswipe
  17. Mysterious Bacteria(唯一质因子解+素数筛)
  18. http://www.blogbus.com/eastsun-logs/7762285.html
  19. 数学基础_设随机变量X1,X2,…Xn相互独立,且都服从(0,θ)上的均匀分布。求U=max{X1,X2,…Xn}数学期望
  20. TCP拥塞控制技术 与BBR的加速原理

热门文章

  1. 在RobotFramework--RIDE中把日期转化为整型进行运算
  2. php 基础 自动类型转换
  3. laravel 分页和共多少条 加参数的分页链接
  4. 把AspDotNetCoreMvc程序运行在Docker上-part3:使用独立的存储容器
  5. ClamAV学习【6】—— cli_load函数浏览
  6. Yii 2.0 权威指南(1) 第一次问候
  7. Linux服务器异常关机,重启启动后weblogic无法启动
  8. Vue之.sync 修饰符详解
  9. 面试硬核干货:纯CSS实现垂直居中,快来收藏吧
  10. Less/Sass 定制私人常用方法库