C++ 使用openMP的多线程双功能
我绝对是多线程和使用OpenMP的新手。但是我正在开发一个有两个功能的程序C++ 使用openMP的多线程双功能,c++,multithreading,openmp,C++,Multithreading,Openmp,我绝对是多线程和使用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
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);
}
lastPicNr=0;
如果(Fg_acquirex(Fg,ncimport,GRAB_INFINITE,ACQ_STANDARD,_memoryAllc)有多种方法可以做到这一点。如果您的编译器至少支持OpenMP 3.0,则可以使用任务指令
VisualStudio只有OpenMP 2,它没有任务指令。我所做的是使用线程库。因为要显示一个映像,您可能需要考虑SDL。它具有与pTrx几乎相同的线程功能,并且使用简单(虽然不像OpenMP那么简单)。
然后您就有了一个库,它可以显示图像、执行线程,并且是跨平台的。读取数据通常是通过单线程完成的,通过并行模式实现是不正确的。此外,为了刷新图像,u可能会使用某种算法(例如平滑函数)在映像上。您应该并行化该部分。并行调用函数将为每个处理器加载映像的全部负载。并行化代码的“核心”,就是分配此工作负载。