title: printf calls messed up with std::thread but std::cout are fine
body: <p>When printf (stdio.h) is used, "Start both threads\n" and "Enter a number\n" mixed together (something like "StEanterrt b a oth thnureaber\nads\n"), but this does not happen in std::cout (iostream). I suspect this has something to do with std::thread, but I am still new with multi-threading programming.</p>
  
  <p>I really don't want to use iostream because it makes the program super large!</p>
  
  <p>I am using mingw32 g++ 4.9.2, compile with <code>g++ -o foreback foreback.cpp -O2 -std=c++11</code>. (Although my computer is 64-bits, but I find out that mingw-w64 produces program about twice the size by mingw32, so I didn't use it).</p>
  
  <pre><code>//#define IOS
  #ifdef IOS
  #include &lt;iostream&gt;
  #endif
  #include &lt;stdio.h&gt;
  #include &lt;thread&gt;
  #include &lt;atomic&gt;
  #include &lt;windows.h&gt; // for Sleep()
  
  std::atomic&lt;int&gt; atom(1);
  
  void foreground() {
      int c = 1;
      while (c) {
  #ifdef IOS
          std::cout &lt;&lt; "Enter a number: ";
          std::cin &gt;&gt; c;
  #else
          printf("Enter a number: ");
          scanf("%d", &amp;c);
  #endif
          atom.store(c, std::memory_order_relaxed);
      }
  }
  
  void background() {
      FILE *out = fopen("foreback.txt", "w");
      int c = 1;
      while (c) {
          fprintf(out, "%d", c);
          c = atom.load(std::memory_order_relaxed);
          Sleep(500);
      }
      fclose(out);
  }
  
  int main() {
      std::thread f(foreground), b(background);
  #ifdef IOS
      std::cout &lt;&lt; "Start both threads.\n";
  #else
      printf("Start both threads.\n");
  #endif
      f.join();
      b.join();
  #ifdef IOS
      std::cout &lt;&lt; "End of both threads.\n";
  #else
      printf("End of both threads.\n");
  #endif
      return 0;
  }
  </code></pre>