title: GNU Readline (libreadline): Displaying output message asynchronously
body: <p>While using readline (blocking) for user input, I would like to output lines of text to the console asynchronously from another thread.  Further, I would like that the readline prompt and current partial input line be removed from the console, the output line written, then the readline prompt and the partial user line restored - so as to give the appearance that the output was written "above" the prompt.</p>
  
  <p>By what combination of readline redisplay functions (or otherwise) can this be achieved? </p>
  
  <p>(Redisplay function documentation: <a href="http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC35" rel="nofollow">http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC35</a>)</p>
  
  <p><strong>problem demo</strong>:</p>
  
  <pre><code>    #include &lt;readline/readline.h&gt;
      #include &lt;readline/history.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;pthread.h&gt;
  
      bool run = true;
  
      void* log_thread(void*)
      {
              while (run)
              {
                      sleep(1);
                      // WHAT TO DO HERE?
                      write(1, "tick\n", 5);
              }
      }
  
      int main()
      {
              pthread_t t;
              pthread_create(&amp;t, 0, log_thread, 0);
  
              while (true)
              {
                      char* p = readline("? ");
                      free(p);
  
                      if (!p)
                              break;
              }
  
              run = false;
              pthread_join(t,0);
      }
  </code></pre>
  
  <p><strong>build</strong>:</p>
  
  <pre><code>$ g++ -pthread -lreadline test.cpp
  $ ./a.out
  </code></pre>
  
  <p><strong>observed output</strong>: (input "foo\nbar\n" typed slowly)</p>
  
  <pre><code>? tick
  ftick
  otick
  otick
  
  ? tick
  tick
  bartick
  tick
  
  ? tick
  ^C
  </code></pre>
  
  <p><strong>desired output</strong>: (input "foo\nbar\n" typed slowly)</p>
  
  <pre><code>tick
  tick
  tick
  tick
  tick
  ? foo
  
  tick
  tick
  tick
  tick
  tick
  ? bar
  
  tick
  ? ^C
  </code></pre>