c++ - 为什么我会出现比赛条件?
问题描述
我正在尝试将多个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_toCombine
一个在哪里std::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();
}
不幸的是,尽管格外小心,我还是遇到了内存访问冲突或与任何一个mesh1
或mesh2
析构函数相关的错误。
我错过了什么吗?
解决方案
而是使标准库的检查功能复杂化:
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);
}
推荐阅读
- html - HTML5 Geolocation 在 Android Oreo 上运行缓慢
- installation - Inno Setup 5.6.0 关于快捷方式和管理员 v 用户的警告
- python - Python 无法在导入的包上打开文件
- c# - 如果此函数不调用,如何将值设置为 lambda
- nginx - 如何从 nginx 中的 url 中删除尾部斜杠?
- javascript - 如何使用套接字 IO 在 Python 客户端和 NodeJS 服务器之间进行通信?
- regex - 正则表达式仅匹配模式前后的一个字母
- excel - 允许用户选择要导入的文本文件
- react-redux - 不影响 redux store 状态的调度操作
- javascript - 使用 Laravel Mix 调用 mix.react 和 mix.js 时 Vue.js 不起作用