opencv::parallel_for_使用说明


直接上代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <functional>

using namespace std;
using namespace cv;

class show{
public:
    show(
        int _x,
        int _y,
        int _z
        ):
        x(_x),y(_y),z(_z) {}
    void showxyz( const Range &range );   //里面的参数必须是Range类型

private:
    int x;
    int y;
    int z;
};


void show::showxyz(const Range &range) {
    for( auto i=range.start; i<range.end; i++ )  //range.start和range.end数值不定,线程自动调整
    {
        cout << "x=" << x << endl;
        cout << "y=" << y << endl;
        cout << "z=" << z << endl;
    }
}

int main( int argc, char **argv )
{
   show test1(4,5,6);  //定义一个类
   //开启线程,线程数不定.让showxyz运行5次
   //这里用bind函数,是因为内部是std::function<void(const Range&)> 类型
   parallel_for_( Range(0,5), std::bind( &show::showxyz, &test1, placeholders::_1 ) );
    return 0;
}

注意:showxyz总体是运行5次,而不是更多,尽管里面有for循环,原因在上面已说明
cout只是为了演示,运行时会出现乱行,正常现象


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM