Using Sleep() while waiting for input

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Do you use Sleep() for your input polling

Yes
5
20%
No
20
80%
 
Total votes : 25

Re: I cannot poll stdin !

Postby Chan Rasjid » 22 Mar 2006, 06:26

Thanks Scotti, Sven,

Finally a big silliness and headache cleared ! I remember now that scanf() waits until I press "enter" so fgets() should wait similarly for a stop signal.

I went through some past posts and now managed to put together the
shortest unsafe input polling on planet earth; just one line "pollInput".

Code: Select all
// courtesy of Uri thru Dieter

void input_init(void){
   DWORD dw;
//PeekNamedPipe(0,....) fails as stdin ( handle 0 ) has been redirected ?
    input_handle = GetStdHandle(STD_INPUT_HANDLE);
   is_pipe = !GetConsoleMode(input_handle, &dw);
}

void pollInput(HANDLE input_handle, searchInfo_t * searchInfo){
   int i;
   unsigned long l;
   char line[256];
   char command[256];
   
   if (PeekNamedPipe(input_handle, line, 255, &l, NULL, NULL) && l > 0){
      while ( (i = sscanf(line,"%s\n",command)) == 1){
         if (!strcmp(command, "new")){
            longjmp(searchInfo->env, 1);
         }
         if (!strcmp(command, "?")){//move now
            longjmp(searchInfo->env, 1);
         }
         if (!strcmp(command, "result")){
            longjmp(searchInfo->env, 1);
         }
         if (!strcmp(command, "quit")){
            longjmp(searchInfo->env, 1);
         }
         strcpy(command, strchr(line, '\n') + 1);
         strcpy(line, command);
      }
   }
   
}

   if (!(++node & pollFrequency) ){
#ifndef      _DEBUG
      pollInput(input_handle, searchInfo);
#endif
   }


Best Regards,
Rasjid
Chan Rasjid
 
Posts: 73
Joined: 23 Feb 2005, 16:36
Location: Singapore

Re: I cannot poll stdin !

Postby Alessandro Scotti » 22 Mar 2006, 15:30

Sven Sch?le wrote:I propose to check the page of Alessandro Scotti, he has nice code for this stuff.

Sven

Edit: In the same minute I see that Alessandro himself proposes the opposite (to look at Crafty et al) but I really think that his own code is a good option because it seems to be written to work not only for one specific engine.


Hi Sven,
LOL it can be said that I do not follow my own advice! :-)
Actually I still like that code, but for the new engine (and considering also this thread) I decided to try the "classic" polling approach so that's the newest thing for me! :-)
It works quite well except for Windows, when in my opinion the behavior when running in a console is not acceptable. Since I don't remember anyone has ever raised this issue, maybe it's just me.
The behavior is this: if you have the engine say analyzing a position and you change focus from or to this console a couple of times, the engine stops thinking. Also, typing a command in the middle of a search (say "stop" for UCI engines) does not always work.
I don't like this so Hamsters uses a very simplified threaded input only when running in a Windows console.
User avatar
Alessandro Scotti
 
Posts: 306
Joined: 20 Nov 2004, 00:10
Location: Rome, Italy

Re: I cannot poll stdin !

Postby Alvaro Begue » 22 Mar 2006, 17:00

I have only skimmed over this thread, but in case anyone is interested, this is how I check if there is input available:

On POSIX systems:
Code: Select all
#include <sys/time.h>

bool is_there_input(){
  fd_set read,write,except;
  FD_ZERO(&read);
  FD_ZERO(&write);
  FD_ZERO(&except);
  FD_SET(0,&read); // stdin

  struct timeval timeout;
  timeout.tv_sec=0;
  timeout.tv_usec=0;

  return select(1,&read,&write,&except,&timeout);
}


On Windows:
Code: Select all
#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;
}


The Windows version only works if stdin is actually a pipe, so it will work if your program is run from a GUI, but not from a console. I do all my development on Linux, so this doesn't matter much.
Alvaro Begue
 
Posts: 33
Joined: 03 Aug 2005, 23:48
Location: Stony Brook, New York, U.S.A.

Previous

Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 22 guests