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