Author: Scott Gasch
Date: 14:00:39 12/27/01
Go up one level in this thread
On December 27, 2001 at 14:25:09, Russell Reagan wrote:
...
>In other words, I could implement a
>multithreaded application using critical sections, and I could do it using
>mutexes, and the other methods, but I don't know if I'm done after I get my
>program working with only one of those schemes.
>
>Are critical sections used for a certain problem, and mutexes used for another,
>and semaphores used for yet another? Or are they all an attempted solution to
>the same problem? And do I need to use more than one of these in my program, or
>will one of these methods solve the problem? And if one will do, are there any
>that would serve a chess program better than the others?
All of these things (and more!) are mutual exclusion devices. From your reading
I'm sure you understand that they are used to acquire a lock and prevent other
threads of execution from accessing some public resource.
One of the cheapest ways to accomplish this is with interlocked operations. If
you are developing under win32, look at InterlockedIncrement,
InterlockedCompareExchange, etc. These are very fast and will accomplish mutual
exclusion within one process (among threads).
Critical sections (I'm talking win32 CRITICAL_SECTIONs here) are another way to
do mutual exclusion within one process. The difference between a critical
section and interlocked operations is that a critical section is more expensive
and allows a blocking wait. With interlocked stuff you have to spin in a loop
trying to get the lock. If the lock is already possessed by another thread, the
thread trying to acquire it will spin in this loop until the lock is released.
This, of course, burns cycles. It's a good idea to keep the size of the
critical code (between getting the lock and releasing it) minimal always but
especially when using interlocked operations.
lStartVal = UNLOCKED_VALUE;
lNewValue = LOCKED_VALUE;
while(InterlockedCompareExchange(&lLock,
lNewValue,
lStartValue) != lStartValue)
{
;
>
>From what I gather, on a single processor machine, using only one of these
>methods should work. But what new problems enter the scene on a multiprocessor
>machine and what solutions exist for those new problems? I am currently on a
>single processor machine, but I'd like to move to a multiprocessor machine in
>the future, so I'd like to have the framework *basically* in place to allow for
>a smooth transition.
>
>Thanks for your help.
>
>Russell
This page took 0.01 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.