title: Why does multiple calls to printf following a pragma omp critical produce garbled output?
body: <p>I have two programs that spawn multiple threads and print <code>hello &lt;TID&gt; world &lt;TID&gt;</code> to stdout.
  The first prints it in one function:</p>
  
  <pre><code>#include &lt;omp.h&gt;                                                
  #include &lt;stdio.h&gt;                                               
  int main() {                               
  #pragma omp parallel                       
    {                                        
      int ID = omp_get_thread_num();         
  #pragma omp critical                                  
      printf("hello  %d world %d\n", ID, ID);                                  
    }      
    return 0;                                  
  }    
  </code></pre>
  
  <p>The second in two:</p>
  
  <pre><code>#include &lt;omp.h&gt;                                                  
  #include &lt;stdio.h&gt;                                           
  int main() {                               
  #pragma omp parallel                       
    {                                        
      int ID = omp_get_thread_num();         
  #pragma omp critical                       
      printf("hello  %d", ID);             
      printf("world %d\n", ID);                                                
    }  
    return 0;                                      
  }      
  </code></pre>
  
  <p>With the program with a single call to <code>printf()</code>, the output is never garbled. 
  However, with the second program, two calls to <code>printf()</code>, the output is garbled.
  With the second program, there is never garbling that interferes with the string sequences; never something of the sort <code>hhhhellloohello...</code>.  There is however garbling between the distinct output strings; a permutation of the possibilities:</p>
  
  <pre><code>hello  27hello  62hello  52hello  50hello  10world 62...
  </code></pre>
  
  <p>as an example.</p>
  
  <p>I thought <code>#pragma omp critical</code> would assert mutual exclusion following the <code>pragma</code> until the end of the parallelised block. But that does not seem to be the case.
  Does <code>#pragma omp critical</code> only apply to the statement immediately following it?</p>
  
  <p>Any insight why this occurs would be great.
  Izaak</p>