c++ - 使用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 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);
            }

最佳答案

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

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

那么您就有了一个可以显示图像、执行线程并且是跨平台的库。

关于c++ - 使用openMP的多线程两个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15975890/

相关文章:

c++ - 多个枚举是否由换行符分隔然后逗号总是可移植的?

multithreading - 从线程访问包变量

c++ - 标量代码和并行代码之间的不同行为

linux - FORTRAN 内存利用率 - 静态与动态

c++ - OpenMP:并行运行两个函数,每个函数占线程池的一半

c++ - C++编程错误

C++(控制台)构造函数和派生类

c++ - Pthread 条件变量不可预测的结果

python - 在 wxPython 中使用线程连续更新 GUI 的好方法吗?

multithreading - 为什么在返回 `Future::poll` 后没有重复调用 `NotReady` ?