title: OpenMP parallelization and deleting from a vector of lists
body: <p>gang. First, a high-level description of the problem &amp; approach.</p>
  
  <p>I have a list containing images and pixel locations in each image - a list of lists. I want to pick <em>n</em> items at random from that list of images and for each image I want to iterate over <em>k</em> random pixel locations. I want to do this in parallel. For each processed pixel, I wish to delete it from the list.</p>
  
  <p>My approach is to distribute the images and pixel lists among all threads - so each thread has its own list of images and lists of pixel locations, but no two threads will be processing the same image at the same time. I store these into a vector.</p>
  
  <p>So let's say the code looks something like this:</p>
  
  <pre><code>struct MyObject
  {
    // Image index on disk
    int imageIndex_;
    // List of x,y locations
    std::list&lt; Point &gt; pixels_;
  };
  
  std::vector&lt; std::list &lt; MyObject &gt; &gt; mainList(NUM_THREADS);
  </code></pre>
  
  <p>Then, <code>mainList[0]</code> would contain the images to be processed by thread with id 0. 
  I launch threads the following way: <code>#pragma omp parallel num_threads(numThreads_)</code> and then they all run the same piece of code which samples images randomly from the thread's list of images.</p>
  
  <p>The problem is, when a pixel is processed and a thread erases it from the pixel list, such as <code>mainList[0].begin()-&gt;pixels_.erase(someIter)</code>, I <strong>sometimes</strong> get a assertion; it traces to the delete operator.</p>
  
  <p>I know that writing to std::list is not thread-safe, but I was pretty sure that it is safe for a list of lists, where each list in the main list is accessed by one thread only. I know that I provided limited code, but the problem boils down to deleting from a list of lists (or vector of lists) in parallel, when each thread has access to only one list at a time and the lists are not shared between threads.</p>
  
  <p>What am I missing here? Can I not delete from a vector of lists of lists in parallel?</p>