Page 1 of 1

OpenMP and STL

Posted: Mon Dec 31, 2012 3:22 am
by sajis997
Hello forum,

I am having problem(segmentation fault) with the following code snippet:

Code: Select all

   //create a pointer to the hitpoint
   HitPoint hitPoint;

   //loop over all the pixels in the image
#pragma omp parallel for schedule(dynamic,1), private(hitPoint)
   for(int y = 0; y < height; ++y)
      for(int x = 0; x < width; ++x)
	 hitPoint = traceAndCreateHitPoint(x,y);

	 //store the hit point inside the vector

      #pragma omp critical
      // Print progress approximately every 5%.
      if ((y+1) % (height/20) == 0 || (y+1) == height)
         std::cout << (100*(y+1)/height) << "% done" << std::endl;      

Is there any issue that need to be resolved when using openmp with STL. If i comment the STL command , the code runs fine.


Re: OpenMP and STL

Posted: Mon Jan 07, 2013 8:15 am
by MarkB
None of the STL containers are guaranteed safe to access using multiple threads in this way. You need to make sure only one thread at a time calls push_back(): putting the call in a critical section should work, though this may not be the most optimal solution.