title: Joining multiple threads in any order
body: <p>I have an array of <code>std::thread</code> objects that it doesn't matter what order they operate in and what order they rejoin the main thread. I've tried using </p>
  
  <pre><code>for(int i = 0; i &lt; noThreads; ++i)
  {
      if(threads[i].joinable())
          threads[i].join();
  }
  </code></pre>
  
  <p>but that <em>seems</em> to make them operate "in order", of course that could be the fact that i'm reaching that conclusion from the fact that the console output from the threads is happening in the order that I dispatch them in (as in all of the output from thread #1 then all of the output from thread #2). I have also attempted <code>threads[i].detach()</code>, but I don't know the execution time of each thread, so I can't pause the program until they have finished.</p>
  
  <p>The work that each thread is doing is:</p>
  
  <pre><code>int spawn(const char* cmd)
  {
      FILE *fp = popen(cmd, "r");
  
      char buff[512];
  
      if(vFlag == 1)
      {
          while(fgets(buff, sizeof(buff), fp)!= NULL)
          {
              printf("%s", buff);
          }
      }
  
      int w = pclose(fp);
      if(WIFEXITED(w))
          return WEXITSTATUS(w);
      else 
          return -1;
  }
  </code></pre>
  
  <p>where the command being executed is <code>"./otherApp.elf"</code>. Bearing in mind that the current fact that it is being printed out to console will change to being output to various files, how do I dispatch the threads so that I can make them execute then rejoin out of order?</p>