Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Hey, I do that!

Author: Robert Hyatt

Date: 07:59:38 03/09/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.


fgets() must block since it can't proceed until the I/O has been completed.
And I doubt any C library would implement this as a polling loop.

But what is your _other_ thread doing when it has nothing to do?  Is _it_
polling waiting on the I/O thread to read a move and pass it over?



>
>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;
>          }


What is the purpose for that loop?  Any such loop will burn CPU resources
until the necessary condition for exit is reached.  You should can any
"loops" that wait for some condition (here !inputwaiting) because that will
smoke the CPU.  In posix threads, I would use a condition variable and
wait on it instead, which blocks.  Another thread can set such a condition to
cause this thread to unblock if it wants...

Not sure how to do that in windows however...  but it is obviously possible.





>        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!



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.