问题
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 ;
}
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
回答1:
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 :)
来源:https://stackoverflow.com/questions/21155658/using-pclvisualization-in-different-threads-from-different-instance-of-a-cla