Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Threads

Author: Greg Lazarou

Date: 16:35:33 04/07/99

Go up one level in this thread


Hi James,

Well if you happen to have access to MFC you can use semaphores and to lock and
unlock a critical resource. Basically the problem is to somehow check and reset
(or set) a flag in one indivisible operation. Here is some of my code looking
into a command queue; here we are in the engine looking for UI commands in the
command queue:

CommandElem * RemoveQueueCommand()
{
	// the lock for accessing the queue
	CSingleLock CmLock(&CommandSemaphore);
	CommandElem * ptrLocal=NULL;

	CmLock.Lock();
		if (CommandQ.IsEmpty() == 0) {
			ptrLocal = (CommandElem *)CommandQ.RemoveHead();
		}
	CmLock.Unlock();

	return ptrLocal;
}

Somewhere in the other thread (in my case the UI or main thread) there is code
that puts stuff in the queue like this:

void QueueSimple(CommandEnum Comm)
{
	POSITION pos;

	// pointer to a command element
	CommandElem * ptrCommand;

	ptrCommand = new CommandElem;
	ptrCommand->TheCommand = Comm;
	commLock.Lock();
		pos = CommandQ.AddTail(ptrCommand);
	commLock.Unlock();

}

The key is that both sets of locks refer to the same global semaphore (must be
called CommandSemaphore).

Now I'm probably wasting some time here but I try not to check the queue too
often (I shoot for 5-10 times per second which is adequate to look responsive to
a human's actions, but does not steal too much performance from the engine's
loop).

Again, all of this applies and you can try it with very little effort if you
have the ability to use MFC (I'm sure Borland's OWL has some similar classes to
the semaphores and locks used above...)

My advice though, is to ignore all of this stuff, forget pondering and stop
improving JRCP; how will Skaki ever catch up if you keep this nonsense up? :-)

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.