在多个线程中同时进行CGAL计算

时间:2015-10-31 22:33:09

标签: c++ multithreading class cgal

我试图在多个线程中同时使用CGAL运行多个几何计算。所有线程都是100%"隔离"彼此。然而,应用程序因访问冲突而崩溃:

http://s8.postimg.org/52u9qphdx/error.png

我已经构建了一个小示例应用来重现错误:

的main.cpp

#include <fstream>
#include "windows.h"
#include "cTestClass.h"

int main ( int argc, char *argv[] ){

    cTestClass *eval0 = new cTestClass("d://.partion_2.stl", 0);
    cTestClass *eval1 = new cTestClass("d://.partion_2.stl", 1);

    eval0->start_eval();
    eval1->start_eval();

    while (!eval0->eval_finished())
    {
       Sleep(200);
    }
    while (!eval1->eval_finished())
    {
       Sleep(200);
    }
    return EXIT_SUCCESS;
}

cTestClass.h

#ifndef CTEST_CLASS_H
#define CTEST_CLASS_H

#include <atomic>
#include <thread>

class cTestClass
{
private:
    std::atomic<bool> flag_eval_finished;
    std::thread eval_thread;
    std::string stl_file_name;
    unsigned int thread_index;

    cTestClass(){ }
    void evaluate_partition();

public:
    cTestClass(std::string argStlFile, unsigned int argThread_index);
    void start_eval();
    bool eval_finished(); 
};

#endif

cTestClass.cpp

#include "cTestClass.h"

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/polygon_soup_to_polyhedron_3.h>
#include <CGAL/IO/Polyhedron_builder_from_STL.h>

#include <iostream>
#include <fstream>

//typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
typedef CGAL::Nef_polyhedron_3<Kernel>  Nef_polyhedron_3;
typedef CGAL::Point_3<Kernel> Point_3;

//typedef Polyhedron::Point_iterator Point_iterator;
typedef Polyhedron_3::Vertex_iterator Vertex_iterator;
typedef Polyhedron_3::Facet_iterator Facet_iterator;
typedef Polyhedron_3::Halfedge_around_facet_circulator Halfedge_facet_circulator;
typedef Nef_polyhedron_3::Volume_const_iterator Volume_const_iterator;
typedef Kernel::Iso_cuboid_3 Iso_cuboid;

//constructor
cTestClass::cTestClass(std::string argStlFile, unsigned int argThread_index)
{
    thread_index = argThread_index;
    stl_file_name = argStlFile;
    flag_eval_finished = false;
}

//===========================================================================================
// public member functions
void cTestClass::start_eval()
{
    std::cout << "Thread " << thread_index << ": start" << std::endl;
    eval_thread = std::thread(&cTestClass::evaluate_partition, this);
}

bool cTestClass::eval_finished()
{
    return flag_eval_finished;
}

//===========================================================================================
//private member functions
void cTestClass::evaluate_partition()
{

    //read stl file and create cgal polyhedron
    Polyhedron_3 poly_Partition; 
    std::ifstream stl_file(stl_file_name.c_str(), std::ifstream::in);
    std::vector<CGAL::cpp11::array<double, 3> > points;
    std::vector<CGAL::cpp11::array<int, 3> > triangles;
    CGAL::read_STL(stl_file, points, triangles);
    stl_file.close();

    //create polyhedron
    CGAL::polygon_soup_to_polyhedron_3(poly_Partition, points, triangles);


    //convert polyhedron to nef_poly representation
    std::cout << "Thread " << thread_index << ": " <<"Converting partition polyhedron to Nef representation" << std::endl;
    Nef_polyhedron_3 nefpoly_Partition(poly_Partition);

    flag_eval_finished = true;
}

任何人都知道造成这次崩溃的原因是什么?

祝你好运

蒙彻

0 个答案:

没有答案
相关问题