title: Using Mutex and lock_guard with a vector in C++
body: <p>I am a newbie in Threading in C++. I need your help regarding usage of <code>mutex</code> and <code>lock_guard</code> (this part doesn't matter). I have a main function and a secondary functions. </p>
  
  <p>Please tell me why multithreading doesn't work when I add <code>lock_guard(mtx)</code>;
  When I remove it, it runs faster but wrongly. Can you help me with it?</p>
  
  <p>I need a correct access to the vector <code>vec</code> and enabling threading.</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;thread&gt;
  
  std::mutex mtx;
  
  void threadCall(std::vector&lt;int&gt; &amp;vec, int start, int end){
      std::lock_guard&lt;std::mutex&gt; guard(mtx);
      for(int i=start; i&lt;end; i++)
        vec[i] = i;
  }
  
  void ThreadFunc(std::vector&lt;int&gt; vec){
      std::vector&lt;std::thread&gt; threads(2);
      threads[0] = std::thread(&amp;threadCall, std::ref(vec), 0, 10);
      threads[1] = std::thread(&amp;threadCall, std::ref(vec), 10, 20);
  
      threads[0].join();
      threads[1].join();
  }
  
  int main(){
  
      std::vector&lt;int&gt; vec(20);
      ThreadFunc(vec);
  
      return 0;
  }
  </code></pre>