问题描述:

  • 使用多线程进行点云的实时重建, 在主线程中创建 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. 首次使用批处理框架 Spring Batch ,被震撼到了,太强大...
  2. python爬虫招聘-Python爬虫-爬取招聘网站信息(一)
  3. 纪中2018暑假培训day3提高a组改题记录(混有部分b组)
  4. 13-3 14 NFS
  5. emqx—mqtt消息服务器
  6. 学习笔记-----C++模板类中友元函数重载输出运算符时提示无法解析的外部符号解决方案
  7. MySQL安全等于的介绍
  8. CodeForces - 1256C Platforms Jumping(贪心+构造)
  9. SQL*Loader 详解
  10. java中关于线程的状态属性_深入理解Java多线程与并发框(第①篇)——线程的状态...
  11. 保姆级My SQL-5.7.19安装教程
  12. 登录显示未找到服务器,未找到服务器
  13. 解决 npm 无法安装 devDependencies 下的依赖包的问题
  14. 【树形dp】VK Cup 2012 Round 1 D. Distance in Tree
  15. 流程图绘制软件 EDraw Mind Map
  16. 基于热传递方程和目标规划的高温服装设计
  17. google css兼容,CSS 针对谷歌或者360(Chrome) safari的webkit核心浏览器 兼容性
  18. 【在线分享】考研数学思维导图+高数思维导图+汤家凤重点笔记+武忠祥重点笔记以及高数Xmind思维导图
  19. 基于JAVA幼儿园管理系统计算机毕业设计源码+系统+lw文档+部署
  20. ewb交通灯报告和文件_简易交通灯控制逻辑电路设计报告

热门文章

  1. maven学习七之用户密码修改和添加用户
  2. 元组 与 字典
  3. [日常工作]非Windows Server 系统远程经常断以及提高性能的方法
  4. webpack 使用教程
  5. 2017.10.7 QBXT 模拟赛
  6. MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权
  7. MineCraft note
  8. Form窗体点击关闭按钮并未关闭进程的解决方法
  9. 调用腾讯的API接口
  10. Yii 2.0 权威指南 (8) 配合数据库使用