title: Make Sure All Printf's in a Thread Actually Print
body: <p>I'm running a highly threaded application (500+ threads).  I need to trace some data from them, and to do so I was printing from the thread.  The output is only cut off it seems.  I've also made sure to flush stdout often and I've also tried using a mutex to coordinate output.  None of those solutions have worked.</p>
  
  <p>This is the thread in question:</p>
  
  <pre><code>void* troutine(void* tmp) {
      a = RDTSC();
      chance = Park(state);
      b = RDTSC();
  
      printf("%s.%i.%c : %lli\n", IMPLEMENTATION, *(int*)tmp, 'T', b-a);
  
  
      usleep(RAND(50));
  
      a = RDTSC();
      Leave(chance, state);
      b = RDTSC();
      printf("%s.%i.%c : %lli\n", IMPLEMENTATION, *(int*)tmp, 'T', b-a);
  
      fflush(stdout);
      pthread_exit(NULL);
  }
  </code></pre>
  
  <p>Only about half the print statements actually print, which is the problem.  I need to make sure they all print, the order doesn't matter, and none of the output is interweaved.</p>
  
  <p><strong>EDIT</strong> main.c</p>
  
  <pre><code>for(i = 0; i &lt; 4000; i++) 
      while(!pthread_create(&amp;tmp, NULL, &amp;troutine, (void*)&amp;testNum));
  </code></pre>
  
  <p>The while loop is so that I ensure the creation of 4k threads as sometimes pthread_create fails with so many threads active.  Also, even when I only set the loop to make i &lt; 4 threads, I still get ~300 lines of output (as opposed to 8).  </p>