title: What happens to a thread in a vector when function execution ends?
body: <p>I want to know more about std::thread, and specifically what will happen if I have a vector of threads, and one of the threads finishes executing.</p>
  
  <p>Picture this example:</p>
  
  <p>A vector of threads is created, which all execute the following function:</p>
  
  <pre><code>function_test(char* flag)
  {
      while(*flag == 1) { // Do Something
      }
  }
  </code></pre>
  
  <p>'char* flag' points to a flag signalling the function to stop execution.</p>
  
  <p>Say, for example, the vector contains 10 threads, which are all executing. Then the flag is set to zero for thread number 3. (The 4th thread in the vector, as vector starts from zero.)</p>
  
  <p>Good practice is to then join the thread.</p>
  
  <pre><code>vector_of_threads[3].join();
  </code></pre>
  
  <p>How many std::threads will the vector now contain? Can I re-start the finished thread with the same function again, or even a different function?</p>
  
  <p>The reason for my question is that I have a vector of threads, and sometimes they will be required to stop executing, and then execution "falls off the end" of the function.</p>
  
  <p>One solution to restart that thread would (I assume, perhaps incorrectly?) be to erase that element from the vector and then insert a new thread, which will then begin executing. Is this correct though, since when a thread stops, will it still be inside the vector? I assume it would be?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>'function_test' is not allowed to modify any other functions flags. The flags are modified by their own function and the calling function. (For the purposes of this, imagine flag enables communication between main and the thread.)</p>
  
  <p>Does this fix the data-race problem, or is it still an issue?</p>