Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: questions about using fget as waiting loop

Author: Alessandro Scotti

Date: 16:31:02 08/30/04

Go up one level in this thread


[sorry hit Enter by mistake]

On August 30, 2004 at 19:10:38, Alessandro Scotti wrote:

>On August 30, 2004 at 14:40:25, Uri Blass wrote:
>
>>Unfortunately I found that if I call input_available in every node under
>>winboard it is very expensive when if I call input_available in every node under
>>text mode it is not expensive.
>
>...not too much but still expensive! In the solution I designed, input_available
>would not be a function but a simple boolean variable, so checking it is way
>faster.

Here's how it works. At startup I create a thread that looks like this:

while( true ) {
   "read from stdin"; // [1]

   if( "one line is ready" ) {
       input_available = true;

       "wait on input acknowledged event"; // [2]
   }
}

Points [1] and [2] are synchronization points where the thread is actually
blocked by the O/S and consumes zero CPU until something happens.

To get input you would write something like:

    if( input_available ) { // a boolean variable!
        "read and do something with the input";

        "send input acknowledge event"; // unlocks thread blocked on [2]
    }

Although this is a simplified sketch, actual code is not much more complicated.
An example of the "send input acknowledge event" part for Windows:

void System::acknowledgeInput()
{
    inputAvailable = false; // i.e. input_available

    SetEvent( hInputBufferEvent );
}

I have timed this solution about 1000x faster than the fastest polling method I
could find. Not that speed is too important here, as you can choose to poll only
at specified intervals, but it doesn't hurt either.

In practice I also wrap the input_available variable with an inline function, so
I can always replace the mechanism with polling if necessary, e.g. on systems
where I don't have threading code ready, without having to change the rest of
the program. In this case acknowledgeInput() is probably empty.



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.