0

I have some logging software that I've written to capture Images from multiple Point Grey cameras, a GPS and a Phidget sensor.

I've used OpenMP to offload different tasks to different threads. They are the following...

  • Thread 3 - Update GPS values (stored in a struct)
  • Thread 2 - Update Phidget values (stored in a struct)
  • Thread 1 - Grab Images and the latest values from the GPS and Phidget structs

When the program is running it displays the images, gps and accelerometer data fine. I hit the 'L' key and the logging of the data works correctly.

The Problem

I press the 'x' key to set the grabimgs to false to exit the while loop in thread 1. This stops the images being updated. I was hoping this would also update the variable in the other threads and they would also stop... but they don't.

This is the bit I don't understand, I can share data obtained in the other threads (Phidget and GPS data) with thread 1. As soon as new images are obtained from the camera the latest values of the accelerometer and GPS are dumped to a file.

int main() {
    //connection flags
    bool cameras_connected  = false;
    bool phidget_connected  = false;
    bool GPS_connected      = false;
    if (cap.ConnectCameras()) {
        cout << "Connected to FireFly cameras" << endl;
        cameras_connected = true;
    } 
    if (GPS.isReady()) { 
        cout << "Connected to GPS" << endl;
        GPS_connected = true;
    }
    if (Phid.connectPhidget()) { 
        cout << "Connected to Phidget" << endl;
        phidget_connected = true;
    }


bool grabimgs = true;
#pragma omp parallel sections shared(GPSData,PhidgetData,grabimgs,cameras_connected,phidget_connected,GPS_connected)
{
    //---- THREAD 1 ----
    // Camera Capture Thread
    #pragma omp section
    {
        while(grabimgs) {
            vector<cv::Mat> Images;
            if(cap.GrabMats(Images)) {
                //do imshow and logging bits
                //GPS and Phidget data is written and displayed from here
                char key = cv::waitKey(1000/30);
                if (key=='x'){
                    grabimgs = false;
                    doLogging = false;
                } 
            }
        }
    }   //END OF THREAD 1

    //---- THREAD 2 ----
    //Phidget Capture Thread
    #pragma omp section
    {
        if (phidget_connected) {
            while(grabimgs) {
                cout << "grabimgs in thread 2 = " << grabimgs << endl;
                //get Phidget data
                Phid.updateReadings(PhidgetData);
            }
        }
    }   //END OF THREAD 2


    //---- THREAD 3 ----
    //GPS Capture Thread
    #pragma omp section
    {
        if(GPS_connected) {
            while(grabimgs) {
                cout << "grabimgs in thread 3 = " << grabimgs << endl;
                //get GPS Data
                GPS.update(GPSData,FORCE_UPDATE);
            }
        }
    }   //END OF THREAD 3
}// end of OMP
return 1;

}

So why can't I use the grabimgs to instruct the other threads to stop? I'm using C++ on Win7 x64, Visual Studio 2010.

4

1 回答 1

0

您应该flush将变量与其他线程同步:flush(grabimgs).

另请参阅https://computing.llnl.gov/tutorials/openMP/#FLUSH

于 2013-06-04T21:33:54.607 回答