Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C and Linux

Author: Scott Gasch

Date: 16:40:00 04/08/02

Go up one level in this thread


On April 08, 2002 at 16:42:45, Frank Phillips wrote:

>It does not work with the cin.rdbuf() command or (of course) polling the
>underlying file.  I need a replacement for the cin.rdbuf, which I imaged was
>some sort of peek command - without taking characters out of the buffer
>
>
>
>fd_set rfds;
>struct timeval tv;
>int retval;
>
>if (cin.rdbuf()->in_avail() > 1)
>   return (1);
>
>FD_ZERO(&rfds);
>FD_SET(0, &rfds);
>tv.tv_sec = 0;
>tv.tv_usec = 0;
>retval = select(1, &rfds, NULL, NULL, &tv);
>if (retval)
>   return (1);

Hi Frank,

To get around the lame implementation of the select equivalent on win32 I ended
up writing my own input thread / input library.  It's easy to do and very
portable... I'd recommend it.  I just create a thread that requests no input
buffering and then spins waiting on fgets(stdin) to read into a buffer.  When it
reads a line I stick it on a queue and increment the number of things waiting to
be parsed.

The main engine thread, whenever it feels like it, can easily tell: how many
input commands are waiting, "peek" at the next one, or "read" the next one
(dequeueing it).  Some high-priority commands can jump to the front of the
queue.  The input thread can respond instantly to some xboard commands and drop
the input, never needing to bother the searching thread.  The only platform
dependent code involved is:

1. Creating the thread (CreateThread and pthread library stuff)
2. Some type of per-thread mutual exclusion (CRITICAL_SECTION on win32 and
pthread stuff again on UNIX).

The performance hit is nominal -- the thread spends most of its life blocked
waiting on input.  My win32 critical section has a small spinlock on it to
optimize thread interaction on MP machines.

Before I gave up and wrote this I was worried that it would have the same issues
as I was trying to overcome and would end up being a big waste of time.  In
retrospect I wish I had done it sooner... I wasted more time dealing with crazy
win32 GetNumConsoleInputs (or whatever it's called).  This, IMHO, a clean and
good solution.  Before you wrangle with...

1. select() on unix, ______ on win32
2. your issue... some non-standard c++ I/O thing which will not port cleanly.
3. crazy mouse / window events in the win32 version of select

...think about giving it a try.

Scott



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.