title: Remove finished threads from vector
body: <p>I have a number of jobs and I want to run a subset of them in parallel. E. g. I have 100 jobs to run and I want to run 10 threads at a time. This is my current code for this problem: </p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;atomic&gt;
  #include &lt;random&gt;
  #include &lt;mutex&gt;
  
  int main() {
      constexpr std::size_t NUMBER_OF_THREADS(10);
      std::atomic&lt;std::size_t&gt; numberOfRunningJobs(0);
  
      std::vector&lt;std::thread&gt; threads;
      std::mutex maxThreadsMutex;
      std::mutex writeMutex;
      std::default_random_engine generator;
      std::uniform_int_distribution&lt;int&gt; distribution(0, 2);
  
      for (std::size_t id(0); id &lt; 100; ++id) {
          if (numberOfRunningJobs &gt;= NUMBER_OF_THREADS - 1) {
              maxThreadsMutex.lock();
          }
          ++numberOfRunningJobs;
          threads.emplace_back([id, &amp;numberOfRunningJobs, &amp;maxThreadsMutex, &amp;writeMutex, &amp;distribution, &amp;generator]() {
              auto waitSeconds(distribution(generator));
              std::this_thread::sleep_for(std::chrono::seconds(waitSeconds));
              writeMutex.lock();
              std::cout &lt;&lt; id &lt;&lt; " " &lt;&lt; waitSeconds &lt;&lt; std::endl;
              writeMutex.unlock();
              --numberOfRunningJobs;
              maxThreadsMutex.unlock();
          });
      }
  
      for (auto &amp;thread : threads) {
          thread.join();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>In the for loop I check how many jobs are running and if a slot is free, I add a new thread to the vector. At the end of each thread I decrement the number of running jobs and unlock the mutex to start one new thread. This solves my task but there is one point I don't like. I need a vector of size 100 to store all threads and I need to join all 100 threads at the end. I want to remove each thread from vector after it finished so that the vector contains a maximum of 10 threads and I have to join 10 threads at the end. I think about passing the vector and an iterator by reference to the lambda so that I can remove the element at the end but I don't know how. How can I optimize my code to use a maximum of 10 elements in the vector?</p>