多线程两个功能使用openMP

multi threading two function using openMP

本文关键字:功能 openMP 两个 多线程      更新时间:2023-10-16

我在多线程和使用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++)?如果有人知道,请告诉我。我已经结束了下面的代码,但现在我想让第二部分睡眠每100ms,我怎么做呢?

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);
            }

有多种方法可以做到这一点。如果您的编译器至少支持OpenMP 3.0,则可以使用task指令http://bisqwit.iki.fi/story/howto/openmp/TaskDirectiveOpenmp % 203% 200年只有

Visual Studio只有没有任务指令的OpenMP 2.0。我所做的是使用一个线程库。由于希望显示图像,因此可能需要考虑使用SDL。它具有与pthreads几乎相同的线程函数,并且使用简单(尽管不像OpenMP那么简单)。http://www.libsdl.org/intro.en/usingthreads.html

然后你有一个库可以显示你的图像,做线程,并且是跨平台的。