[Novalug] keyboard/console input blocking problem

Ed James edward.james@gmail.com
Thu Oct 11 16:37:04 EDT 2012


All,

   Man, this is gonna be a pain to describe the problem without pointing to a
screen, but here goes.

  I've got a C++ program that creates an X window via xlib.  I launch it from
a terminal emulator (like xterm) and include a data file for it to read, like:

> myProgram myData.dat

The myData.dat is read line-by-line and each line is a command for myProgram
to execute.  myProgram also reads events as they come through the X Window
event queue (such as button and mouse clicks in the X window that MyProgram
creates) and displays info about each such event onto the screen.  Easy so far.
But I also want to read console/keyboard input.  However, myProgram
blocks on the cin
read from the console until I hit <ENTER>.  This prevents event queue info
from being displayed as it comes from the queue (until I hit <ENTER>).

while (TRUE) {
   read the data file, line by line and execute commands until EOF
   check the event queue, and if there are events, display them
   check if there's any keyboard commands <ENTER>ed
   }
// a "quit" command breaks the above loop

Question - is there a way in Linux to avoid the console/keyboard input
blocking?  I seem to remember being able to do that in the Olde Days
with MS-DOS (conio.h perhaps?).  I'd like to take a peek to see if
there's any keyboard activity before <ENTER> is hit, perhaps some
kind of OS buffer tucked away somewhere.  I'm looking for something
like what exists for blocking vs non-blocking sockets.

Question - if the answer is "use threads!", then is the <pthread.h>
route the best one to take?  I'm used to using threads from my
Java days and understand them pretty well.  But for various
reasons, I'd like to avoid them in this case.  But if I need to
use them, I'd like somethings that is stable and going to be
around for a while.

adTHANKSvance,
Ed James



More information about the Novalug mailing list