【发布时间】:2013-04-12 15:58:36
【问题描述】:
我绝对是多线程和使用 OpenMP 的新手。但我正在尝试开发一个具有两个功能的程序。
function1(){
get the data from the camera and save it into memory for 1000fps
}
functio2(){
display and refresh the screen each 100ms
}
我认为OpenMP应该是这样的
#pragma omp sections
{
{ function1(); }
#pragma omp section
{ function2(); }
}
但我不确定如何在代码 (c++) 中实际实现这一点?如果有人知道,请告诉我。 我最终得到了以下代码,但现在我想让第二部分每 100 毫秒休眠一次,我该怎么做?
lastPicNr = 0;
if(Fg_AcquireEx(fg,nCamPort,GRAB_INFINITE,ACQ_STANDARD,_memoryAllc)<0){
CExceptionHandler::GrabberErrorMessage(fg,"Can not start Acquiring images .");
}else{
//Declaring a parallel region that will be broken down into disctinct parallel sections
#pragma omp parallel sections
{
#pragma omp section
{
while((lastPicNr = Fg_getLastPicNumberBlockingEx(fg,lastPicNr+1,nCamPort,10,_memoryAllc))<= MaxPics){
iPtr=(unsigned char*)Fg_getImagePtrEx(fg,lastPicNr,0,_memoryAllc);
_PointVector.push_back(iPtr);
_lastPicNumber.push_back(lastPicNr);
}
}
#pragma omp section
{
cv::Mat _matrixImage(cv::Size(w,h), CV_8UC1,iPtr , cv::Mat::AUTO_STEP);
cv::imshow("test",_matrixImage);
cv::waitKey(10);
}
}
PauseClickMode(hDlg);
}
【问题讨论】:
-
读取数据通常是通过单线程完成的,通过并行方式实现并不正确。此外,为了刷新图像,您可能会在图像上使用某种算法(例如平滑函数)。您应该并行化该部分。并行调用函数将为每个处理器加载图像的全部负载。并行化代码的“心脏”,您可以分配此工作负载。
标签: c++ multithreading openmp