Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Hey, I do that!

Author: Daniel Clausen

Date: 06:38:08 03/08/02

Go up one level in this thread


On March 08, 2002 at 09:17:44, Robert Pope wrote:

>On March 08, 2002 at 00:27:18, Robert Hyatt wrote:
>
>>
>>One _very_ bad thing CM used to do was poll for input in a tight loop when
>>it was sitting waiting on a move (not pondering).  This meant that it would
>>burn 50% of the CPU while the _other_ program was thinking.  This was a
>>bad bug several years ago, and I have no idea if it was ever fixed or not.
>>
>>If not, that _might_ be what is being discussed.  A tight "got any input?"
>>loop is NFG when the engine could simply block on a read() call...  which
>>would burn no CPU at all.
>
>This is actually the very problem I have with my own chess engine - it uses 100%
> CPU on its move an 50% CPU on the opponent's move, just because of polling.
>This is my first multi-threaded app, so I'm not sure how to fix this (blocking,
>etc.).  I thought my code would not be so hoggy because the polling thread would
>stop at the fgets() and wait for input, but apparently not.
>
>Is there a simple fix for this? Here is the function that the thread runs (via a
>_beginthread call):
>
>void InputThread(void* threadno)
>  {
>    char strIn[256];
>    inputThreadActive++;
>    for(;;) {
>        inputWaiting=false;
>        stringIn[0]=10;
>        fgets(strIn,255,stdin);
>        int i;
>        for (i=0;i<256;i++) {
>            stringIn[i]=strIn[i];
>          }
>        inputThreadActive--;
>        inputWaiting=true;
>        for(;;) {
>            if((!inputWaiting)||(endInputThread)) break;
>          }
>        if(endInputThread) break;
>      }
>    printf("thread ending");
>    _endthread();
>  }
>
>I wanted to keep all my threading simple and in one place, but not at its
>current cost!


If one thread has to wait for a condition, which another thread will fulfill,
semaphores (or mutex which are binary semaphores) are the way to do this. The
basic idea of semaphores is to not poll at full (or 50% speed) but wait and hope
to get kicked as soon as the condition is true. :) Unfortunately there's no
platform-independant way to do this in C. Please contact the manual of your
favuorite compiler. :p (maybe someone can post how do use semaphores in
VisualC++, assuming you're using this)

Sargon



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.