title: How can I safely clear a boost::interprocess:vector in shared memory if other processes could be iterating over it?
body: <p>Let's say I have a boost interprocess vector in shared memory.  One process may be iterating through it.  If another process wants to concurrently clear the vector, what do I need to do so that I don't crash in the process that is doing the iteration?</p>
  
  <p><strong>PROCESS #1</strong> - iterates over vector in shared memory</p>
  
  <pre><code>using namespace boost::interprocess;
  
  typedef allocator&lt;int, managed_shared_memory::segment_manager&gt;  ShmemAllocator;
  typedef vector&lt;int, ShmemAllocator&gt; MyVector;
  
  managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  const ShmemAllocator alloc_inst (segment.get_segment_manager());
  MyVector *myvector = segment.construct&lt;MyVector&gt;("MyVector")(alloc_inst);
  size_t size = myvector-&gt;size();
  for (size_t i = 0; i &lt; size; i++)
  {
    //potential crash when PROCESS 2 calls clear
    int value = myvector-&gt;at(i);
  }
  </code></pre>
  
  <p><strong>PROCESS #2</strong> - can potentially clear the vector in shared memory</p>
  
  <pre><code>using namespace boost::interprocess;
  
  typedef allocator&lt;int, managed_shared_memory::segment_manager&gt;  ShmemAllocator;
  typedef vector&lt;int, ShmemAllocator&gt; MyVector;
  
  managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  const ShmemAllocator alloc_inst (segment.get_segment_manager());
  MyVector *myvector = segment.construct&lt;MyVector&gt;("MyVector")(alloc_inst);
  if (somecondition)
     myvector-&gt;clear();
  </code></pre>
  
  <p>Two things I was thinking:
  copy the vector to local memory std::vector before iterating.   I'm not sure if the copy would be safe, though, either.  Can't imagine it would</p>
  
  <p>or</p>
  
  <p>some sort of interprocess mutex, which for simplicity's sake, I'd like to avoid.</p>