【问题标题】:Multi threading with non-void functions具有非空函数的多线程
【发布时间】:2015-01-20 13:56:05
【问题描述】:

我正在尝试在 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();
}

我想使用线程以并行方式找到嘴巴和眼睛。怎么做?我的问题在于非 void 函数和返回值。 提前致谢。

【问题讨论】:

    标签: c++ multithreading opencv


    【解决方案1】:

    一种简单的方法是使用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();
    }
    

    【讨论】:

    • 我按照你的建议做了,但它给了我这个error: reference to non-static member function must be called auto eyesMatch = async(launch::async, _find_eyes, ref(image), ref(eye)); 和嘴巴一样。有什么建议吗? ——
    • @JackJack 下次发布完整代码。 std::async(std::launch::async, &amp;MyClass::_find_eyes, this, std::ref(image), std::ref(eye))std::async(std::launch::async, &amp;MyClass::_find_mouth, this, std::ref(image), std::ref(mouth))。将 MyClass 替换为您的班级名称。
    猜你喜欢
    • 2021-07-04
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多