具有非空函数的多线程

Multi threading with non-void functions

本文关键字:多线程 函数      更新时间:2023-10-16

我正在尝试在C++中多线程我的程序(我使用 OpenCV 库)这是代码:

double _find_eyes (Mat img, vector<Rect_<int> > & finalEyes)
{
//some code working on image
return valueOfMatch; //is a double
}
double _find_mouth (Mat img, vector<Rect_<int> > & finalMouth)
{
//some code working on image
return valueOfMatch; //is a double
}
double _find_face ()
{
eyesMatch = _find_eyes(image, eye);
mouthMatch = _find_mouth(image, mouth);
totalMatch = eyesMatch + mouthMatch;
}
int main()
{
find_face();
}

我想用线平行地找到嘴巴和眼睛。怎么办?我的问题在于非空函数和返回值。提前谢谢。

一个简单的方法是使用 std::async ,例如:

double _find_face ()
{
    auto eyesMatch = std::async(std::launch::async, _find_eyes, std::ref(image), std::ref(eye));
    auto mouthMatch = std::async(std::launch::async, _find_mouth, std::ref(image), std::ref(mouth));
    return eyesMatch.get() + mouthMatch.get();
}