【问题标题】:multi threading two function using openMP使用openMP的多线程两个函数
【发布时间】: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


【解决方案1】:

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

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

那么你就有了一个库,可以显示你的图像,做线程,并且是跨平台的。

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多