You are in danger of making mutex useless by the way you are thinking/coding.
Code: Select all
mut=CreateMutex()
;The next code is someplace else in the program.
This is the proper way to check for a lock, while not blocking the thread.
If TryLockMutex(mut)
Debug "Was not locked"
;Do some stuff here if needed.
Debug UnlockMutex(mut)
Else
Debug "Was locked"
;Nothing we can do, attempting to unlock here would be pointless
;as in a multi-threaded app it is possible that it's been unlocked already
;by the time this Else block is executed.
;However if this in the cleanup routine of your program,
;AND it is the very last time the mutex is referenced,
;AND all other threads using the mutex has finished,
;THEN you could do a unlock, but only then.
;PS! "A mutex can only be unlocked by the thread that also locked it. " so you need to trigger the cleanup in the thread that made the lock.
EndIf
Polly wrote:BTW: The command TryLock is only senseful "if you don't even know wether your resource is currently locked or not"
Actually no, it's purpose is to allow doing stuff if nothing else is doing stuff.
If Try fails it means something else is busy doing stuff.
LockMutex() is a blocking wait.
TryLockMutex() let you do "other" stuff, or implement you own wait loop.
TryLock is used a lot in parallel processing for example.