Thread overview
Nonblocking IO for stdin
Jan 08, 2007
gareis
Jan 09, 2007
gareis
Jan 11, 2007
Stewart Gordon
January 08, 2007
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.
January 09, 2007
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.
January 11, 2007
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.
<snip>

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.