使用多线程加速处理这一段for循环vector<Edge1D_Result>edges;for (int i = 0; i < m; i++) { edges = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints[i], m_dMeasureLength, m_dMeasureHeight, m_vdMeasureAngle[i] , m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest); for (int i = 0; i < edges.size(); i++) { m_vpdEdgePoints.push_back(edges[i].m_pdEdgePoint); m_vdEdgeGradient.push_back(edges[i].m_dGradient); } }保护变量edges
时间: 2023-12-10 14:40:35 浏览: 152
可以使用互斥锁mutex来保护变量edges。具体操作如下:
1. 在头文件中包含<mutex>。
2. 在for循环外定义一个mutex对象,例如:std::mutex mtx_edges。
3. 在for循环内对edges进行操作时,加锁mtx_edges,操作完成后解锁。
代码示例:
```
#include <mutex>
std::vector<Edge1D_Result> edges;
std::mutex mtx_edges;
for (int i = 0; i < m; i++) {
edges = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints[i], m_dMeasureLength, m_dMeasureHeight, m_vdMeasureAngle[i], m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest);
// 加锁
mtx_edges.lock();
for (int j = 0; j < edges.size(); j++) {
m_vpdEdgePoints.push_back(edges[j].m_pdEdgePoint);
m_vdEdgeGradient.push_back(edges[j].m_dGradient);
}
// 解锁
mtx_edges.unlock();
}
```
阅读全文