Polling standard input from C++

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Polling standard input from C++

Post by bob »

michiguel wrote:
mar wrote:I solved this problem by simply reading stdin in a loop in main thread (blocking) and having engine (=main search) in separate thread.
Portable plus no need to poll within search.
+1000

It is the only approach I ever used. In fact, at the end of last century :-), I learned multithreading programming just to do this (which was not that difficult for something simple like this). Any other approach gave me headaches before starting.

Miguel
SOME of us started when a thread was not an option. :) Crafty ran for several years on MS dos systems. Try to start a thread there. :) Or on single-processor Univac machines. Or IBM machines. Or (until the early 80's) on a Cray...
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Polling standard input from C++

Post by mar »

bob wrote:SOME of us started when a thread was not an option. :) Crafty ran for several years on MS dos systems. Try to start a thread there. :)
Yes but in DOS you could hook IRQs and access I/O ports or enter protected mode (as well as crash the whole thing easily). Can't quite do that in user mode these days :)
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Polling standard input from C++

Post by hgm »

XBoard protocol was designed such that each GUI->engine command given while the engine might be searching will be preceded by a SIGINT. So you don't need to start a separate thread, you only have to catch the signal, and either abort the search in the signal handler, or can read a command without being afraid this will block.

Too bad it did not work on Windows...
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Regarding SIGINT

Post by sje »

Regarding SIGINT (similarly, SIGHUP, SIGQUIT, and SIGTERM)

The general method for handing SIGINT should be:

1) Treat it like SIGKILL and end the process immediately, or

2) Allow the process to clean up a bit and then quickly terminate without further input.

Using it for mostly anything else is just a kludge. I'm currently using it in the random game generator as a test of the catcher/handler, and that code will soon be changed.

----

The question remains as to how best interrupt a chess search. In the early days of programming my 1986 Mac Plus, I used the MouseDown event, polled at each node, to signal an early termination request. Another kludge, but there was little else available at the time.

My idea here is to have the Watcher thread, who is the first responder to standard input, check to see if a designated signalling character (like "!") is entered from the keyboard and then set a flag accessible by the search thread(s). When this flag, polled at each node, is set then the search unwinds gracefully and quickly.

To make this work with a single keystroke and not a keystroke plus a carriage return, then the standard input will have to be persuaded to handle one character at a time (probably via setvbuf()).
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Regarding SIGINT

Post by hgm »

What you describe sounds more like SIGTERM. As the name already suggest SIGINT is for interrupts, which implies later continuation, rather than abort.
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: Regarding SIGINT

Post by syzygy »

SIGINT
The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Control-C, but on some systems, the "delete" character or "break" key can be used.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Polling standard input from C++

Post by bob »

mar wrote:
bob wrote:SOME of us started when a thread was not an option. :) Crafty ran for several years on MS dos systems. Try to start a thread there. :)
Yes but in DOS you could hook IRQs and access I/O ports or enter protected mode (as well as crash the whole thing easily). Can't quite do that in user mode these days :)
Depends on how much you know. :)
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Polling standard input from C++

Post by mar »

bob wrote:Depends on how much you know. :)
I'm all ears :)
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Polling standard input from C++

Post by bob »

mar wrote:
bob wrote:Depends on how much you know. :)
I'm all ears :)
I am not sure you would want to run as root and install your own device driver that would let you do ANYTHING you want in the kernel... :)
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Polling standard input from C++

Post by mar »

bob wrote:I am not sure you would want to run as root and install your own device driver that would let you do ANYTHING you want in the kernel... :)
Yes I expected a driver :) I guess we can agree that separate thread is not such a bad idea after all ;)