title: Running an std::async in a loop. How to get consistent results?
body: <p>I try to parallelize calculations and write the results in order to a vector.</p>
  <p>I'm using a <code>std::async</code>. The program works perfectly with a short pause (ref. the commented line). Without this pause, the program crashes.</p>
  <p><a href="https://godbolt.org/z/vs8n3j" rel="nofollow noreferrer">The Code <em><sub>( in an online IDE to modify &amp; test the MCVE </sub></em>:</a></p>
  <pre><code>class AsyncDispatcher {
   public:
    /* Data is a custom class with data that contains a ID.
     * The object contains no
     * links or pointers.*/
    vector&lt;Data&gt; calcResult(vector&lt;Params&gt; params) {
      vector&lt;Data&gt; result;
      vector&lt;std::future&lt;NumberedData&gt;&gt; futures;
  
      threadID = 0;
      for (auto param : params) {
        futures.push_back(((std::async(std::launch::async, [&amp;]() {
          this_thread::sleep_for(chrono::milliseconds(100));  // not workable without this command 
           //^^^^^^^^^^^^^^^^^^^^^^^^^
          return NumberedData(filler.createData(param, threadID), threadID);
        }))));
        threadID++;
      }
  
      for (auto&amp; f : futures) {
        result.push_back(f.get().data);
      }
      return result;
    }
  
    AsyncDispatcher() : threadID(0) {}
  
   private:
    int threadID;
    Filler filler;//fills an Data with numbers
  };
  
  </code></pre>