title: std::vector, thread-safety, multi-threading
body: <p>I am using std::vector as shared data in a multi-threaded application. I encapsulate the thread inside a class, e.g., </p>
  
  <pre><code>class ABC {
  public:
      double a, b, c;
  };
  
  boost::mutex mutex1;
  
  class XYZ {
  public:
      XYZ(vector&lt;ABC&gt; &amp; pVector) {
          ptrVector = &amp;pVector;
          m_thread = boost::thread(&amp;XYZ::Start, this);
      }
      ~XYZ() {}
      void Start();
  public:
      vector&lt;ABC&gt; * ptrVector;
      boost::thread m_thread;  
  };    
  
  void XYZ::Start() {
      try {
          while(1) {
              boost::this_thread::interruption_point();
              for (unsigned int i=0; i&lt;ptrVector-&gt;size(); i++) {
                  {
                      boost::mutex::scoped_lock lock(mutex1);
                      ptrVector-&gt;at(i).a = double(rand())/10000;  
                      ptrVector-&gt;at(i).b = double(rand())/10000;
                      ptrVector-&gt;at(i).c = double(rand())/10000;
                  }
              }
          }
      }
      catch(boost::thread_interrupted) {}
      catch(std::exception) {} 
  }
  </code></pre>
  
  <p>When I close the application, <strong>sometimes</strong>, in the debug, there will be 2 error messages, <strong>sometimes</strong> there will be no error messages. I often heard people talking about std::vector being not thread-safe, is this one of the cases? I am using Visual Studio 2008, boost thread, the size of the vector is fixed. Can anyone also offer some advice on how to use std::vector in a multi-threaded application. </p>
  
  <ol>
  <li>First-chance exception at 0x7688b9bc in ETP.exe: Microsoft C++
  exception: std::out_of_range at memory location 0x02d8f7bc..</li>
  <li>First-chance exception at 0x00e916e0 in ETP.exe: 0xC0000005: Access
  violation reading location 0x00000008.</li>
  <li>Second Chance Assertion Failed: File c:\program files
  (x86)\microsoft visual studio 9.0\vc\include\vector, Line Second
  Chance Assertion Failed: File c:\program files (x86)\microsoft
  visual studio 9.0\vc\include\vector98</li>
  </ol>
  
  <p>Thanks.</p>