Threading in C
Posted: 09 Apr 2007, 15:43
Hi all
Although my C knowledge is limited, I have decided to write my chess engine is C. It now has a fairly quick move generation (12x12 board technique), alpha-beta with quiescence search on captures/promotions (using MVV/LVA for ordering). It also keeps track of 3-fold repetition using Zobrist keys with the aid of a dedicated position cache. As you can see I still have a lot to do!
I am now playing around with getting Winboard to work, deciding to use threads for that purpose. I got a basic I/O thread working, but I am struggling to get it to terminate gracefully as the thread is stuck waiting for input when it should be terminating (so actually I cannot get it to terminate at all!). The basic outline is as follows.
Any ideas or pointers would be greatly appreciated. BTW, I am keeping a blog of my progress on the engine if you're interested.
Thanks for the help!
Although my C knowledge is limited, I have decided to write my chess engine is C. It now has a fairly quick move generation (12x12 board technique), alpha-beta with quiescence search on captures/promotions (using MVV/LVA for ordering). It also keeps track of 3-fold repetition using Zobrist keys with the aid of a dedicated position cache. As you can see I still have a lot to do!
I am now playing around with getting Winboard to work, deciding to use threads for that purpose. I got a basic I/O thread working, but I am struggling to get it to terminate gracefully as the thread is stuck waiting for input when it should be terminating (so actually I cannot get it to terminate at all!). The basic outline is as follows.
- Code: Select all
#define BUFFERSIZE 10
CRITICAL_SECTION section;
char buffer[BUFFERSIZE][100];
int terminate;
void reader(void *arg)
{
char tmp[100];
while (!terminate)
{
scanf("%s", tmp);
/* if buffer full, sleep a while and test again ... */
EnterCriticalSection(§ion);
/* add to buffer... */
LeaveCriticalSection(§ion);
}
}
int readui(char* data)
{
/* if buffer empty, return 0... */
EnterCriticalSection(§ion);
/* get next input and copy into data... */
LeaveCriticalSection(§ion);
return 1;
}
int winboard()
{
HANDLE hThread;
InitializeCriticalSection(§ion);
hThread=(HANDLE)_beginthread(reader, 0, NULL);
/* While game has not ended, do game stuff... */
terminate = 1;
printf("\nTerminating...");
/*?? and right hereabouts it hangs.... ??*/
WaitForSingleObject(hThread, INFINITE);
DeleteCriticalSection(§ion);
}
int main()
{
winboard();
}
Any ideas or pointers would be greatly appreciated. BTW, I am keeping a blog of my progress on the engine if you're interested.
Thanks for the help!