title: Outputting while getting input
body: <p>I would like to be able to output information while inputting.
  For example: Printing a line every second, but also taking user input...</p>
  
  <p>I tried:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  using namespace std;
  
  void foo()
  {
      while(1)
      {
          usleep(1000000);
          cout &lt;&lt; "Cake\n";
      }
  }
  
  int main()
  {
      thread t1(foo);
      t1.join();
      string x;
      while(1)
      {
          cin &gt;&gt; x;
          cout &lt;&lt; x &lt;&lt; "\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>Which outputs:</p>
  
  <blockquote>
    <p>Cake<br>
    Cake<br>
    Cake<br></p>
  </blockquote>
  
  <p>And then I start typing:</p>
  
  <blockquote>
    <p>hiCake</p>
  </blockquote>
  
  <p>When I would like it to be:</p>
  
  <blockquote>
    <p>Cake<br>
    hi</p>
  </blockquote>
  
  <p>Where the 'hi' is still in the input</p>
  
  <p>If this isn't possible, is there at least a way for me to pause the outputting while there is text being inputted?</p>
  
  <p>I'm using C++11 on Windows 7 using CygWin for the Unix libraries</p>
  
  <p><strong>MUTEX TESTS</strong></p>
  
  <p>Output loop locked:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mutex&gt;
  using namespace std;
  
  mutex mtx;
  
  void foo()
  {
      mtx.lock();
      while(1)
      {
          usleep(1000000);
          cout &lt;&lt; "Cake\n";
      }
      mtx.unlock();
  }
  
  int main()
  {
      thread t1(foo);
      t1.join();
      string x;
      while(1)
      {
          cin &gt;&gt; x;
          cout &lt;&lt; x &lt;&lt; "\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>Input loop locked:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mutex&gt;
  using namespace std;
  
  mutex mtx;
  
  void foo()
  {
      while(1)
      {
          usleep(1000000);
          cout &lt;&lt; "Cake\n";
      }
  }
  
  int main()
  {
      thread t1(foo);
      t1.join();
      string x;
      mtx.lock();
      while(1)
      {
          cin &gt;&gt; x;
          cout &lt;&lt; x &lt;&lt; "\n";
      }
      mtx.unlock();
      return 0;
  }
  </code></pre>
  
  <p>Pure output locked:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mutex&gt;
  using namespace std;
  
  mutex mtx;
  
  void foo()
  {
      while(1)
      {
          mtx.lock();
          usleep(1000000);
          cout &lt;&lt; "Cake\n";
          mtx.unlock();
      }
  }
  
  int main()
  {
      thread t1(foo);
      t1.join();
      string x;
      while(1)
      {
          cin &gt;&gt; x;
          cout &lt;&lt; x &lt;&lt; "\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>Pure input locked:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mutex&gt;
  using namespace std;
  
  mutex mtx;
  
  void foo()
  {
      while(1)
      {
          usleep(1000000);
          cout &lt;&lt; "Cake\n";
      }
  }
  
  int main()
  {
      thread t1(foo);
      t1.join();
      string x;
      while(1)
      {
          mtx.lock();
          cin &gt;&gt; x;
          mtx.unlock();
          cout &lt;&lt; x &lt;&lt; "\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>To save space, I won't paste more code; I did other tests where I made a <code>mutex mtx2</code> and tried to lock both input (<code>mtx</code>) and output(<code>mtx2</code>) (both pure and the loops). I did also try locking both with <code>mtx</code> (again, both pure and loops)</p>