title: Copying std::vector between threads without locking
body: <p>I have a vector that is modified in one thread, and I need to use its contents in another. Locking between these threads is unacceptable due to performance requirements. Since iterating over the vector while it is changing will cause a crash, I thought to copy the vector and then iterate over the copy. My question is, can this way also crash?</p>
  
  <pre><code>struct Data
  {
      int A;
      double B;
      bool C;
  };
  
  std::vector&lt;Data&gt; DataVec;
  
  void ModifyThreadFunc()
  {
      // Here the vector is changed, which includes adding and erasing elements
      ...
  }
  
  void ReadThreadFunc()
  {
      auto temp = DataVec;    // Will this crash?
      for (auto&amp; data : temp)
      {
          // Do stuff with the data
          ...
      }
  
      // This definitely can crash
      /*for (auto&amp; data : DataVec)
      {
          // Do stuff with the data
          ...
      }*/
  }
  </code></pre>
  
  <p>The basic thread safety guarantee for <code>vector::operator=</code> is:</p>
  
  <blockquote>
    <p>"if an exception is thrown, the container is in a valid state."</p>
  </blockquote>
  
  <p>What types of exceptions are possible here?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I solved this using double buffering, and posted my answer below.</p>