我在 4 个不同的 Mat 对象上调用了 4x 这个 opencv 图像处理函数。
void processBinary(Mat& binaryMat) {
//image processing
}
我想要对其进行多线程处理,以便所有 4 个方法调用同时完成,但让主线程等待,直到每个线程完成。
Ex:
int main() {
Mat m1, m2, m3, m4;
//perform each of these methods simultaneously, but have main thread wait for all processBinary() calls to finish
processBinary(m1);
processBinary(m2);
processBinary(m3);
processsBinary(m4);
}
我希望实现的是能够根据需要多次调用 processBinary() ,并且具有与仅调用该方法一次相同的效率。我查过多线程,但对调用线程然后加入/分离它们有点困惑。我相信我需要实例化每个线程,然后在每个线程上调用 join() ,以便主线程等待每个线程执行,但执行时间似乎没有显着增加。谁能解释我应该如何对我的程序进行多线程处理?谢谢!
EDIT: 我尝试过的:
//this does not significantly increase execution time. However, calling processBinary() only once does.4
thread p1(&Detector::processBinary, *this, std::ref(m1));
thread p2(&Detector::processBinary, *this, std::ref(m2));
thread p3(&Detector::processBinary, *this, std::ref(m3));
thread p4(&Detector::processBinary, *this, std::ref(m4));
p1.join();
p2.join();
p3.join();
p4.join();