Dynomotion

Group: DynoMotion Message: 7994 From: Toby Rule Date: 7/25/2013
Subject: MutexLock/MutexUnlock
Hi Tom,

How does MutexLock/MutexUnlock work?  It seems that no matter what value I give for an argument in MutexLock, it always blocks.  

For instance, the following code always blocks:

#include "KMotionDef.h"

main()
{
MutexLock(100);
MutexUnlock(100);
}

Thanks,

Toby
The information contained in this transmission is intended only for the person or entity
to which it is addressed and may contain II-VI Proprietary and/or II-VI Business Sensitive
material. If you are not the intended recipient, please contact the sender immediately
and destroy the material in its entirety, whether electronic or hard copy. You are
notified that any review, retransmission, copying, disclosure, dissemination or other
use of, or taking of any action in reliance upon this information by persons or entities 
other than the intended recipient is prohibited.
Group: DynoMotion Message: 7996 From: Tom Kerekes Date: 7/25/2013
Subject: Re: MutexLock/MutexUnlock
Hi Toby,

A Mutex is an integer variable that is zero if the resource is available and if not available then the value is the Thread number + 1 of the Thread that "owns" the resource.

You must pass the address of the Mutex to the Lock/Unlock routines.  For multiple threads to have access to the same global mutex the simplest method is to use a persist.UserData integer.  So code something like:

#include "KMotionDef.h"

#define MUTEXVAR 199

main()
{
    MutexLock(&persist.UserData[MUTEXVAR]);
    MutexUnlock(&persist.UserData[MUTEXVAR]);
}
HTH
Regards
TK