title: Two threads blocking on I/O
body: <p>Let's consider the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  int main()
  {
      std::thread t1([](){
          int a;
          std::cin &gt;&gt; a;
          std::cout &lt;&lt; "T1: " &lt;&lt; a &lt;&lt; std::endl;
      });
      std::thread t2([](){
          int a;
          std::cin &gt;&gt; a;
          std::cout &lt;&lt; "T2: " &lt;&lt; a &lt;&lt; std::endl;
      });
  
  
      t1.join();
      t2.join();
      return 0;
  }
  </code></pre>
  
  <p>Compiled with: <code>g++ -std=c++14 -pthread main.cpp -o main</code>
  Run with: <code>./main &lt; file.txt</code></p>
  
  <p>file.txt: </p>
  
  <pre><code>1 2
  </code></pre>
  
  <p>The outputs are different ( I supposed so), for example:</p>
  
  <pre><code>T1: T2: 12
  
      T1: 1T2: 
  2
  
  T1: 12
  T2: 0
  </code></pre>
  
  <p>and so on.</p>
  
  <p>I suppose that code causes undefined behaviour? Am I right? 
  And how to solve/explain situation when two threads try use the same ( ? ) input/output?</p>