使用"pcl ::可视化"来自一个类的不同实例的不同线程中 [英] using "pcl::visualization" in different threads from different instance of a class

查看:663
本文介绍了使用"pcl ::可视化"来自一个类的不同实例的不同线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个包含在浊点上的可视化器的类. 这是我的代码:

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 thread
        boost::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 ;
}

现在我有一类可视化工具,可以在"main"中实例化.当我从类"my_vis"中仅创建一个实例时,程序运行时一切正常.但是我需要两个或更多实例.当我初始化多个实例时,发生了错误: BLOCK_TYPE_IS_VALID 我认为这是因为使用了线程.但是线程在我的项目中是必需的.

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.我正在使用PCL 1.7

P.S. I am using PCL 1.7

推荐答案

两天后,我终于解决了这个问题.

After two days, I finally solve this.

我关注pcl :: visualization :: PCLVisualizer的构造函数以及"SpinOnce"函数,并且我认识到,如果您放置了静态锁,那么多个对象中只有一个线程可以访问这些函数,那么问题就来了将得到解决.

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.

以前,我在这些函数上放置了非静态锁,并且您知道本地锁仅在创建它们的同一对象中起作用(而不是从类实例化的整个对象).因此,我在my_vis类中定义了一个静态锁:

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 

并将"vis-> spinOnce(1)"替换为

and replace "vis->spinOnce(1)" with

    { 
            boost::mutex::scoped_lock vis_lock(vis_mutex); 
            vis->spinOnce (); 
    } 

我仍然认为这不是永久性的解决方案,因此该错误最好由pcl开发人员解决:)

I still think that it is not a permanent solution, and this bug is better to be solved by pcl developers :)

这篇关于使用"pcl ::可视化"来自一个类的不同实例的不同线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆