www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Nonblocking IO for stdin

reply gareis <dhasenan gmail.com> writes:
Sorry, I accidentally posted this to .D rather than to .learn. I begin 
to think I never will.

I'm looking for nonblocking input from stdin. Every input function for 
streams blocks, and din.available() always returns 0.

I can do this with a socket, but I don't know how to open a socket for 
stdin.

Any suggestions of where to look? I haven't found anything in the 
archives about it.

Thanks.
Jan 08 2007
parent reply gareis <dhasenan gmail.com> writes:
So I modified Phobos somewhat so I could make a socket to associate with 
stdin and thereby see if there was any input ready. There were two 
problems with this: first, there was a false positive upon starting the 
program, so it hanged for input; second, it did not acknowledge any 
further keyboard input.

The only solution I see currently is to use threads. That should be 
relatively safe, given that the input thread should never need to 
interact with the output thread. I hope.

gareis wrote:
 Sorry, I accidentally posted this to .D rather than to .learn. I begin 
 to think I never will.
 
 I'm looking for nonblocking input from stdin. Every input function for 
 streams blocks, and din.available() always returns 0.
 
 I can do this with a socket, but I don't know how to open a socket for 
 stdin.
 
 Any suggestions of where to look? I haven't found anything in the 
 archives about it.
 
 Thanks.

Jan 08 2007
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
gareis wrote:
<snip>
 The only solution I see currently is to use threads. That should be 
 relatively safe, given that the input thread should never need to 
 interact with the output thread. I hope.

I think using threads is indeed the solution. I haven't tried it, but expect that it would be perfectly safe as long as communication through stdin, stdout and stderr is always done through only one thread. Keeping both input and output in one thread will help to ensure that the input and output don't become hopelessly tangled with each other in the console window. Stewart.
Jan 10 2007