重点:
paralle_for_设置成n个线程,则实际只有n-1线程并行,第n个线程会等待其他线程运行结束后再执行,所以n=1和n=2实际上都是串行
cv::parallel_for_是opencv封装的一个多线程接口,利用这个接口可以方便实现多线程,不用考虑底层细节,以下是一个具体的例子
继承ParallelLoopBody,重载运算符()
class LoopBody : public cv::ParallelLoopBody
{
public:
LoopBody (const std::vector<std::string>& filenames)
: m_filenames(filenames)
{
}
virtual void operator()(const cv::Range& r) const
{
for (int i = r.start; i != r.end; i++) //遍历
{
std::cout << m_filenames[i] << std::endl;
//load image and to to sth
}
}
protected:
const std::vector<std::string>& m_filenames;
};
启动循环
LoopB