为什么我会出现竞争条件?

Why am I getting a race condition?

我正在尝试将多个 CGAL 网格合并为一个几何体。

我有以下运行良好的顺序代码:

while (m_toCombine.size() > 1) {
    auto mesh1 = m_toCombine.front();
    m_toCombine.pop_front();
    auto mesh2 = m_toCombine.front();
    m_toCombine.pop_front();

    bool result = CGAL::Polygon_mesh_processing::corefine_and_compute_union(mesh1, mesh2, mesh2);

    m_toCombine.push_back(mesh2);
}

其中 m_toCombinestd::list<Triangle_mesh_exact>

Triangle_mesh_exact 是一种 CGAL 网格(三角多面体几何)。但我认为这与问题无关。

不幸的是,这个过程对于我的预期应用来说太慢了,所以我决定使用“分而治之”的概念并以并行方式组合网格:

class Combiner
{
public:
    Combiner(const std::list<Triangle_mesh_exact>& toCombine) :
        m_toCombine(toCombine) {};
    ~Combiner() {};

    Triangle_mesh_exact combineMeshes();
    void combineMeshes2();

private:
    std::mutex m_listMutex, m_threadListMutex;
    std::mutex m_eventLock;

    std::list<MiniThread> m_threads;
    std::list<Triangle_mesh_exact> m_toCombine;
    std::condition_variable m_eventSignal;

    std::atomic<bool> m_done = false;

    //void poll(int threadListIndex);
};


Triangle_mesh_exact Combiner::combineMeshes()
{
    std::unique_lock<std::mutex> uniqueLock(m_eventLock, std::defer_lock);

    int runningCount = 0, finishedCount = 0;

    int toCombineCount = m_toCombine.size();

    bool stillRunning = false;
    bool stillCombining = true;

    while (stillCombining || stillRunning) {
        uniqueLock.lock();

        //std::lock_guard<std::mutex> lock(m_listMutex);
        m_listMutex.lock();
        Triangle_mesh_exact mesh1 = std::move(m_toCombine.front());
        m_toCombine.pop_front();
        toCombineCount--;
        Triangle_mesh_exact mesh2 = std::move(m_toCombine.front());
        m_toCombine.pop_front();
        toCombineCount--;
        m_listMutex.unlock();

        runningCount++;

        auto thread = new std::thread([&, this, mesh1, mesh2]() mutable {
            //m_listMutex.lock();
            CGAL::Polygon_mesh_processing::corefine_and_compute_union(mesh1, mesh2, mesh2);
            
            std::lock_guard<std::mutex> lock(m_listMutex);
            m_toCombine.push_back(mesh2);
            toCombineCount++;
            finishedCount++;
            m_eventSignal.notify_one();
            //m_listMutex.unlock();
        });
        thread->detach();
        
        while (toCombineCount < 2 && runningCount != finishedCount) {
            m_eventSignal.wait(uniqueLock);
        }

        stillRunning = runningCount != finishedCount;
        stillCombining = toCombineCount >= 2;

        uniqueLock.unlock();
    }
    
    return m_toCombine.front();
}

不幸的是,尽管我格外小心,但我遇到了内存访问冲突崩溃或与 mesh1mesh2 析构函数相关的错误。

我是不是漏掉了什么?

反而使标准库的检查功能复杂化:

std::reduce - cppreference.com

Triangle_mesh_exact combine(Triangle_mesh_exact& a, Triangle_mesh_exact& b)
{
    auto success = CGAL::Polygon_mesh_processing::corefine_and_compute_union(a, b, b);
    if (!success) throw my_combine_exception{};
    return b;
}

Triangle_mesh_exact combineAll()
{
    if (m_toCombine.size() == 1) return m_toCombine.front();
    if (m_toCombine.empty()) throw std::invalid_argument("");
    return std::reduce(std::execution::par,
                       m_toCombine.begin() + 1, m_toCombine.end(), 
                       m_toCombine.front(), combine);
}