title: Why is std::cout preempted in a thread but printf is not?
body: <p>I am starting to learn threads in the C++11 standard and I was trying out a very basic program which creates 10 threads, joins them and then exits. In the thread function, I tried to print out the index of the for loop that I'm creating the threads in, like this:</p>
  
  <pre><code>std::vector&lt;std::thread&gt; threads;
  for(int i = 0; i &lt; 10; i++)
  {
      threads.push_back(std::thread([i](){ printf("Thread #%d\n", i); }));
  }
  </code></pre>
  
  <p>And this produces an output expected from a concurrent program, the threads execute out of order:</p>
  
  <pre><code>Thread #0
  Thread #2
  Thread #1
  Thread #3
  Thread #4
  Thread #5
  Thread #6
  Thread #7
  Thread #8
  Thread #9
  </code></pre>
  
  <p>But when I try to do the same thing using <code>std::cout</code> and <code>std::endl</code>, I get this:</p>
  
  <pre><code>Thread #0
  Thread #Thread #2
  Thread #3
  Thread #9
  1
  Thread #8
  Thread #4
  Thread #5
  Thread #7
  Thread #6
  </code></pre>
  
  <p>Why is this happening with <code>std::cout</code> but not with <code>printf</code>?</p>