Why Recursive Mutex is required | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why Recursive Mutex is required

Hi I have created code for recursion without recursive mutex as below: https://code.sololearn.com/cEat29GHmanL Correct me if I am doing something wrong. Now question is that I have put recursive call outside of mutex lock and unlock.... It is not mandatory to have recursive call as critical section and hence recursive_mutex is not required...Right? Or else is there any need to have such scenario? If yes, would like to have some example for my better understanding.

23rd Nov 2020, 8:27 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
Hi Ketan Lalchet I am not an expert on c++11 or later, but I found that mutex and recursive_mutex offers proteccion for threads, i see that in mutex documentation they post that the thread is locked, but on the next iteration on the funtion, that value still on the same level locking*, but recursive_mutex offers a new (higer) level of locking, trying to lock an object allready locked. A reason for use it I think it should be for data secure, or something like errors handle. I will like read an answer so much deeper of your cuestion. Sory for bad english.
24th Nov 2020, 10:31 PM
MICAEL MIRANDA
MICAEL MIRANDA - avatar
+ 1
Yeah that's true that recursive is more safer as it handles another lock by same thread...only concern is lock and unlock count should match... Only concern is about use case as recursion also worked fine for my case without recursive_mutex
25th Nov 2020, 2:07 PM
Ketan Lalcheta
Ketan Lalcheta - avatar