[computer-go] Hello / Pondering

Álvaro Begué alvaro.begue at gmail.com
Tue May 1 19:32:28 PDT 2007


On 5/1/07, Darren Cook <darren at dcook.org> wrote:
> > smaller for larger boards. The only part of our program that is not
> > strictly ANSI C++ compliant is is_there_input(), ...
> > ...
> >  return select(1,&read,&write,&except,&timeout);
> > ...
> > If you are interested on a Windows equivalent, I might be able to
> > provide one.
>
> Hi Alvaro,
> I'm interested in a windows equivalent, though for a program nothing to
> do with computer go. Basically select() on windows does not work for
> stdin (it only works for sockets created with winsock).
>
> The code I found to use is:
>
> DWORD stdin_timeout=100;    //In milliseconds, so sleep for 0.1s.
> HANDLE stdin_handle=GetStdHandle(STD_INPUT_HANDLE);
> DWORD stdin_wait_result=WaitForSingleObject(stdin_handle,stdin_timeout);
> if(stdin_wait_result==WAIT_OBJECT_0){
>   //There is input on stdin
>   }
> else{
>   //Stdin is quiet
>   }
>
> (For a pondering go program you want stdin_timeout set to 0.)
>
> However, I suspect this code is actually blocking (sometimes?). In
> addition behaviour seems to be different in each of:
>   * Using it from the console
>   * Using it where stdin is connected to a pipe (also see [1])
>   * Using it under cygwin, over ssh.

Yes, this is true. The version I wrote (this was for a chess program
using UCI), was only good for the case of a pipe, which is the
relevant one if you plug your program to a GUI. If case this might be
what you are looking for, here it is:

#include <windows.h>

bool is_there_input(){
  static DWORD available;
  available = 0;
  static HANDLE stdin_h = GetStdHandle(STD_INPUT_HANDLE);
  PeekNamedPipe(stdin_h, NULL, 0, 0, &available, 0);
  return available > 0;
}

>
> Most of what google has turned up for me so far is that if you want to
> reliably read from stdin in a non-blocking way you should use threads.
> Like you, this is a path I don't want to go down, especially as
> select(stdin) works fine on linux, but it is looking like I have no choice.
>
> Darren
>
> [1]:
> http://communicator.sourceforge.net/sites/MITRE/distributions/GalaxyCommunicator/contrib/MITRE/utilities/src/stdin_utility.c
>
>
> _______________________________________________
> computer-go mailing list
> computer-go at computer-go.org
> http://www.computer-go.org/mailman/listinfo/computer-go/
>


More information about the computer-go mailing list