Computer Chess Club Archives


Search

Terms

Messages

Subject: Here is one way...

Author: Greg Lazarou

Date: 20:06:58 02/19/99

Go up one level in this thread


On February 19, 1999 at 19:35:14, John Coffey wrote:

>I am using Microsoft Visual C++ with MFC to develope my front end.  I figure
>that I need a way to spawn my engine in a seperate process, and then somehow
>communicate between the two.  Can anyone please tell me how to do this?
>
>John Coffey
>

In my program I have two threads; one to process input commands (from winboard
or the user) and another to do the engine work (pondering, analyzing,
thinking...) In the main program I start the engine thread like this:

// try to start the engine
if (AfxBeginThread(Engine, (LPVOID)NULL, THREAD_PRIORITY_NORMAL) != NULL) {
	strOut = "Engine started\n";
	PrintOut(strOut);
...

The engine is a routine that starts like this:

UINT Engine(LPVOID pParam)
{
...

Threads can communicate with each other via global variables and structures but
to safeguard structure sanity you should use semaphores to control the access of
any shared resources. In my case I have a queue of commands that I use for the
main program to pass to the engine and I use one semaphore for this and then
another semaphore to protect/sequence the outputs to stdout (back to winboard).
The semaphores are defined like this:

CSemaphore OutputSemaphore;
CSemaphore CommandSemaphore;

and an example of using them as locks to access something safely is:

CSingleLock OLock(&OutputSemaphore);

OLock.Lock();
	cout << (LPCTSTR)strOut;
	cout.flush();
OLock.Unlock();

Basically, look at the CSemaphore and CSingleLock classes of MFC for all the
info...

If you need to see more code/examples, drop me an e-mail request and I'll send
you some more code. Good luck...

Greg




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.