Mutex behaviour question

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Mutex behaviour question

Post by jassing »

Is this expected/as-designed?

Code: Select all

h=CreateMutex()

Procedure test( h )
  LockMutex(h)
  Debug "t1"
  LockMutex(h)
  Debug "t2"
EndProcedure

LockMutex(h)
Debug "M1"
LockMutex(h)
Debug "M2"

UnlockMutex(h) : UnlockMutex(h) ; both are required.  It acts like a semaphore in this regard.	
WaitThread(CreateThread(@test(),h))
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Mutex behaviour question

Post by STARGÅTE »

jassing wrote: Wed May 29, 2024 10:59 pm Is this expected/as-designed?
Yes.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Mutex behaviour question

Post by Caronte3D »

So... PB uses "Recursive Mutex"... Interesting :idea:
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Mutex behaviour question

Post by Fred »

Yes, we use recursive mutex
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Mutex behaviour question

Post by jassing »

Thanks! Just wanted to be sure I could rely on it, it was unexepected.
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mutex behaviour question

Post by skywalk »

Interesting, I primarily use semaphores, but what I read says recursive mutexes are not preferred and rarely adopted.
There are small code cases where a recursive mutex can prevent a deadlock, but the deadlock was only possible due to a bad algorithm.

Anyway, it is worth documenting this within the thread library help.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Mutex behaviour question

Post by STARGÅTE »

The fact that the Mutex can be locked multiple times (and unlocked the same multiple times) allows the call of procedures which contain a Lock/Unlock block individually but also nested.

For example, you have a mutex protected list and define a procedure which sorts all elements.
Of cause, this procedure need a Lock/Unlock.
But you want to define an other procedure which also locks the mutex but calls the sorting procedure as well.
Therefore, the Lock-counter in Mutex is helpful.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Mutex behaviour question

Post by Caronte3D »

skywalk wrote: Thu May 30, 2024 4:43 pm Anyway, it is worth documenting this within the thread library help.
+1000

If I was know about it's recursive, I could use a OutputDebugString_ for each Mutex and UnMutex so I know if a random deadlock is because a non paired mutex.
Post Reply