title: weird output when I use pthread and printf
body: <p>I write a program using <code>pthread</code>.</p>
  
  <p>Environment:windows 7 , CYGWIN_NT-6.1 i686 Cygwin , gcc (GCC) 4.5.3</p>
  
  <p>The source code</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;pthread.h&gt;
  
  void *th_func(void *p)
  {
      int iLoop = 0;
  
      for(iLoop = 0;iLoop&lt;100;iLoop++)
      {
          printf("Thread Thread Thread Thread\n");
      }
  
      return;
  }
  
  int main()
  {
      int iLoop = 0;
      pthread_t QueThread;
  
      printf("Main : Start Main\n");
  
      printf("Main : Start Create Thread\n");
      pthread_create(&amp;QueThread,NULL,th_func,NULL);
      printf("Main : End Create Thread\n");
  
      for(iLoop = 0;iLoop&lt;100;iLoop++)
      {
          printf("Main Main Main Main\n");
      }
  
      pthread_join(QueThread,NULL);
  
      printf("Main : End Main\n");
  
      printf("---------------\n");
  
      return 0;
  }
  </code></pre>
  
  <p>When I compile the source code, there are no warnings or errors,but it's output is weird.</p>
  
  <p>A part of it's output</p>
  
  <pre><code>Main : Start Main
  Main : Start Create Thread
  Thread Thread Thread ThreThread Thread Thread Thread
  Main Main Main Main
  Thread Thread Thread Thread
  Main Main Main Main
  </code></pre>
  
  <p>I want to know the cause of such phenomenon.</p>
  
  <p>In this output, <code>Main : End Create Thread</code> is not printed completely. And at line 3, a newline <code>\n</code> at the end of <code>"Thread Thread Thread Thread\n"</code> disappear.</p>
  
  <p>Is everyone's output like this? It does not occur every time, but occurs sometime.</p>
  
  <p>If I use mutex to call <code>printf</code> safely,the weird output seem to be stopped.</p>
  
  <p>POSIX says <code>printf</code> is thread-safe, and according to Cygwin.com, cygwin provides posix-style API. However, there is the unexpected output. </p>
  
  <p>Is <code>printf</code> really thread-safe?</p>
  
  <p>I executed the same program 100 times in Linux(Ubuntu), and this output did not occur.</p>
  
  <p>In addition, I have not understood the reason why some words on the output disappeared.</p>