title: Why does add function have no effect in c++ 11 thread?
body: <p>I am trying to learn the c++ 11 thread and have following code :</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;mutex&gt;
  #include &lt;algorithm&gt;
  
  void add(int&amp;  i){
      std::mutex some_mutex;
     // std::cout &lt;&lt; &quot; I am &quot; &lt;&lt; std::endl;
      std::lock_guard&lt;std::mutex&gt; guard(some_mutex); 
      i++;
  }
  
  
  int main(){
      int i = 0;
      std::vector&lt;std::thread&gt; vec_threads;
  
      for(int i = 0; i &lt; 10; i++){
          vec_threads.push_back(std::thread(add,std::ref(i)));
      }
  
      std::for_each(vec_threads.begin(),vec_threads.end(),
              std::mem_fn(&amp;std::thread::join));
      std::cout&lt;&lt; &quot; i = &quot; &lt;&lt; i &lt;&lt; std::endl;
  return 0;
  }
  </code></pre>
  <p>I have created a <code>vector</code> that holds <code>std::thread</code> and I call the add function from each thread and pass i by ref.  After what I assumed that the thread would do (the adding of <code>i = i+1</code>), the end result does not reflect what I wanted.</p>
  <hr />
  <blockquote>
  <p>output: i = 0</p>
  <p>expected output: i = 10</p>
  </blockquote>