TryLockMutex

BlitzMax Forums/BlitzMax Beginners Area/TryLockMutex

Retimer(Posted 2010) [#1]
Quick clear-up of this...

TryLockMutex will lock the mutex IF it is not already locked, preventing it from being locked twice? Or does it do something else?


Dreamora(Posted 2010) [#2]
TryLockMutex normally is the same as LockMutex with the difference that it is not blocking, so the code will continue in either case.
To allow the following code to work correctly, will return you a value. That value indicates if it was able to lock it or not.
if it did not succeed you would skip the whole block where you access the "concurrently used data" and where you would unlock the mutex again.

That way you can allow a thread to continue working potentially, assuming it has further work to do.
I'm personally though not aware of cases where you really would have such a thing with Mutexes. With Semaphores where you will get to know how many threads are already waiting, its a different story.