title: Synchronizing STD cout output multi-thread
body: <p>Latelly I've been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a logical order, the program that I'm testing is something like:<br></p>
  
  <pre><code>#include &lt;boost/thread/thread.hpp&gt;
  #include &lt;iostream&gt;
  
  int my01( void )
  {
      std::cout &lt;&lt; "my01" &lt;&lt; std::endl;
      return 0;
  }
  /* my02, my03 and my04 are the same with different outputs*/
  [...]
  int main( void )
  {
      boost::thread t1(&amp;my01);
      boost::thread t2(&amp;my02);
      boost::thread t3(&amp;my03);
      boost::thread t4(&amp;my04);
  
      while(!t1.joinable() || !t2.joinable() || !t3.joinable() || !t4.joinable());
  
      t1.join();
      t2.join();
      t3.join();
      t4.join();
  
      std::cout &lt;&lt; "The end!" &lt;&lt; std::endl;
      getchar();
      return 0;
  }
  </code></pre>
  
  <p><br>
  And the output is usually like (it changes):</p>
  
  <blockquote>
    <p>my02my01<br>
    my04<br>
    my03<br>
    BLANK LINE<br>
    The end!<br></p>
  </blockquote>
  
  <p>With this issue in mind I was thinking of creating a single thread to manage all of the outputs, so they would be in order like:<br></p>
  
  <blockquote>
    <p>my01<br>
    my02<br>
    my03<br>
    my04<br>
    The end!<br></p>
  </blockquote>
  
  <p>Which is the optimal way to write such thread or to manage those outputs?<br>
  Please read the answers to this question too: <a href="https://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe">Is cout synchronized/thread-safe?</a><br><br>
  Ps:I'm using Visual C++ 2010 Express and my cpu has 8 different cores.<br><br>
  Thank you for your time!<br></p>