YouTube Video Transcript

3,286 words

Full Transcript

[Music] hello friend this is rotation of watching Serena's video series on C++ and today's topic is a recursive mutex in C++ threading so this video comes under threading series and we will see a recursive mutex today and this looked like a very big theory part but it is not it is really very easy so let's go through the point 1 but before that I want to give you a little introduction about what is recursive matrix means in bit basic terms so let's suppose you have some recursive function let's make it recursive ok and this is the function and this is calling itself and recursion there is this recursive function call inside this and it is possible that this recursion somewhere here is accessing some critical data in critical section so you will be using mutex to solve that critical section problem right so here you have m dot now first time when you call this you will come here you will go inside this and you will see that ok M is not logged ok before that let's have two threads T 2 and T 1 these two threads were running this recursive function and there was this mutex lock and this thread T 1 happened too long this particular mutex first so this mutex belong to T 1 now then again it will call itself see recursive will again call itself and then again it will come inside and T 1 will see that m1 dot lock it will try to lock this M again but as this is mutex it will say that I am already logged and it will wait for this so this is kind of a deadlock situation because you have the lock I mean this thread have the lock but again this thread is looking for the same lock so this situation should not come however so to get rid of this situation we have this recursive mate X so instead of writing STD musics you'll use a recursive lyrics now everything will be same but when this again called will happen it will check and it will see that ok this M is a recursive mutex and I am the only owner of this lock so what it will do it will allow to lock it again and it will go here and then it will again go here and it will come and it will see that ok it is recursive may attack so keep on going like this ok so there is no problem when this is recursive mutex and one more thing you have to right unlock so that unlock should be here am dot unlock now as you can see that it is always coming here and after this recursive function call it is going back so whatever is after this recursive function was never executed so once the terminating condition will hit it will go back and then it will start executing this m dot unlock so let's suppose this am dot lock was logged for ten times then this m dot unlock will also be called ten times so we are good to go and this is what the requirement is the number of times you lock recursive mutex that many times you have to unlock it otherwise it will be considered still logged by this thread so this is the basic understanding don't worry I will show you two code one with recursion and another with loop yes exactly this scenario is applicable in loops also because it is possible that you have some for loop and inside that you have this critical section and you want to lock it again and again actually you don't want to lock it again and again but your situation is you cannot get rid of this mutex lock statement and whatever you want to access is inside loop so you have to lock it again in again so that is the problem with this scenario that your critical section this is let's suppose critical section then this critical section is inside loop right and if you are accessing this critical section by mutex first time then again this loop will come back and you have to again lock it and if it is normal mutex you will end up having deadlock because you are going to lock it again I mean you're going to wait for this mutex by holding the lock of the same mutex it is something like this you have this mutex in your pocket and you're saying that if someone will give me this particular mutex I will go further so that is not going to happen right because you already have that particular mutex in your pocket and no one can give you the same mutex what is there in your pocket correct so this is a problem and the solution is this recursive mutex so let's go line by line and try to understand what I have written as a nodes here so the first point is it is same as mute expert same thread can lock one mutex multiple time using recursive mutex that's what I explained here if it is recursive matrix you can go ahead and lock as number of time you want second point is if thread t1 first called lock or try lock on recursive mutex m1 then m1 is locked by t1 only now as t1 is running in the recursion t1 can call log or try log any number of time there is no issue that's what I said if t1 has logged it first then t1 can recursively lock it n number of time no problem so second point is I mean third point is but if t1 have acquired 10 times either using lock or try log method on mutex m1 then thread t1 will have to unlock it 10 times that's what I explained here if you are locking your recursive mutex 10 time in recursion then you have to unlock it also in 10 times you have to write your logic in such a way that you will end up unlocking it 10 time otherwise that mute would be considered lock so this is very important line it means recursive mutex keeps count how many times it was logged so that many time it should be unlocked yes this is very important find how many times this lock was called this M is very intelligent this will keep the track how many time I was called if it is recursive mutex okay then only it can track that okay I was unlocked that many time okay so the last point is how many time we can lock liquor semantics is not defined yes exactly this recursion is I mean this level of the recursion is not defined so you can lock this may be thousand times in some system and in some system it may be way more than that it depends on the system how much stack it have okay because this recursion works on stack right so how much space you have that many time you can actually go ahead and lock this correct and wait a minute this is very important point what was that how many times we can call the recursive matrix is not defined that's what I said but when that number reaches and if we were calling lock it will return STDs system error we have to catch it or if we were calling try lock it will return false so I have given try lock on mutex it is like you will try to lock the mutex if you cannot lock it you won't wait for that mutex so this is the logic behind try lock and lock is it will try to lock if it is not able to lock it will wait for that mutex to be unlocked by some another thread and then it will go ahead and lock it so in both the cases these are the respective outputs okay then enough theory let's look at the practical then we'll go for the bottom line actually these are the similar sort lines of these bigger lines okay so let's go for the practicals so this is your program here first we will see this program and then we will go for the loop one okay so this is fairly simple program you can see that we have two threads one two we have this thread ID zero and one I am passing this as thread I D so that I can print this ID and we'll get to know that okay what thread is inside this and I have this 10 and 10 and this 10 is saying that how many times this recursion will run okay because I'm checking this loop for is less than zero then I will return okay so you should be knowing how recursion works to understand this little more okay so let's compile it logically so let's assume t1 was started first and it entered inside this and look for is not less than zero that's 10 for the first time m1 dot lock m1 is a recursive mutex yes this is the syntax to write the recursive mutex and t1 actually logged it one Stephen logged it you know that t2 was also about to start this recursive function as a thread and the moment even logged it and reached here t2 was actually reached here and t2 also tried to lock this but t2 cannot lock this because t1 have already logged it so t2 will wait for this mutex to get unlocked okay so this t2 will wait here while t1 will actually keep on working so by t1 we will print this C which is 0 so 0 buffer plus plus this is some global variable which will be acting as a critical section in between these two threads and after implementing this buffer we are calling ourself recursion with the same C and this time loop for minus minus so this time 9 will go inside this and again will come here and 9 is less than 0 yes it is false it will again come back it will try to log this mutex and it can actually lock because it is a recursive mutex and it will go ahead and keep on doing this until unless this loop becomes minus and if this is minus it will hit return so it will return it and then it will unlock it and then that stack will be empty and again it will return for 0 1 three four five till like that it will return for almost ten times and it will unlock this mutex for ten time so let me show you this by running this and if you don't understand this I am going to I'll explain you how actually it works okay so before that let's go ahead and run this so that I can show you that this is a working code so I compiled it it has compiled let's run this okay if you will see this our output is 0 4 0 0 4 1 so this first zero is a thread ID okay let me just write that recompile this and execute it see for thread ID 0 it is printing 0 1 2 3 till 10 and then from 11 to 1 is incremented by thread number 1 and thread number 1 was actually able to lock this because sorry thread number 2 was able to lock it ok there are so much confusion with this 0 & 1 let me just make this one and do and recompile this so thread 1 did its job and as I said it have to unlock it how many time it is locking it so as looking at this number it is locking this mutex for 11 times from 0 to 10 so it has to unlock 11 times then only t2 can actually lock them so let's do one thing when it is going to unlock it let's give this message okay then we will come to know that which thread is locking and which thread is unlocking it see it is very crisp and clear that initially it was locked by a thread 1 and it was coming here till this location and it will again call itself so this thread ID statement was printed 0 to 10 time after this this for loop would have become false and it went back so and back means it came here and then after that it will start executing this one so see it printed that unlocked by thread 1 and it printed this for time 1 2 3 4 5 6 7 8 9 10 11 see it unlocked 11 time then only thread 2 logged it see then thread 2 also have to unlock it 11 time this is what it is okay so let's see how this actually work so let's suppose this t1 called first so there is this bucket for t1 and as I said it will come inside this it will check whether it is true or false so loop 4 will be initially 2 so 2 less than 0 is false so it will not return it will come inside this and m1 dot lock will be performed and one dot lock is performed it will print lock by thread ID so I will write in short order log by thread ID and ID is 1 so 1 and it will print this buffer okay so initially buffer is 0 so it will print 0 and after this it is hitting Rickerson so there is this again our easy recursion function called ok so it will create another stack similar to this again we will check but this recursion is not called empty it is called with loop 4 and C so we can assume C is always 1 in this case and loop 4 is decremented by 1 so loop 4 was 2 this time it will go as 1 so we will have 1 less than 0 which is again false so it will not go inside this return it will again try to log this and I am dot lock was performed and then again it will print logged by thread ID so logged by 1 and this time this buffer it was increment it by 1 so it will be 1 and then again it will hit this recursion so let's write this our EC and this time this recursion C will be always 1 and this loop 4 will become 0 now okay you remember this loop 4 came as 1 so 1 minus minus will become 0 so we are passing 0 inside another function and one function call will create one stack so this is first stacks again stag third stack similarly 0 less than 0 no it is not true so mutex will be logged again b1 and this time this buffer will become 2 because we are always incrementing it ok so this has become 2 now and again we will have this are EC this time it will be minus 1 because we got 0 here okay inside this function call ok so 0 minus minus will become minus 1 and then we will call function with minus 1 and this minus 1 less than 0 is true okay see this condition will become true this minus 1 is less than 0 yes then it will return from here and when this returns it goes back where it was called so after this what is left it was this one and this one so after this these two statement was left so now it will execute these two so m1 dot unlock so m1 dot unlock and then it will print unlocked by thread ID so it will print unlocked what is this ID 1 and once it is complete head so this full stack forget about this boundary it is actually this much big ok so once it is done this full stack is over so how did you eat reach to this stack because of this call remember then again you will go back to this call only ok like this now you had executed this line so you reached and created this stack now you are back so whatever was left off at that moment you will execute that now so what was left m1 dot and allowed here and see out unlocked by and after executing these two this is again done and you will be going back here and similarly here also you went to this stack and two things was left so it will do the same thing again forget about this boundary it will do m dot unlock which is this one and it will print unlock by thread ID one so can you see that how many times you logged it one time two time third time fourth time you went but this condition was false see this condition was false so you was returned you cannot go here you was returned and you came back and then you started executing m dot unlock one time unlock second time unlock third time so can you see that you logged one time two times second time and third time you unlocked one time second time and third time so you logged three times you unlocked three times and this is how recursion work and don't forget to hit the like button if you liked how I explained about the recursion here who it was not easy okay then so as you have unlocked it three times now you remember t 2 was actually waiting here as I said T 1 is this 1 T 2 is also there somewhere which will have the similar things like this and here it will hit return like this it will again go back and then you will execute two line and then it will go back you will again execute two line it will go back you will again execute these two lines these two lines are nothing but these two lines this one and this one and YT 2 was waiting here because mutex can be logged by any of the thread and we assume that thread 1 happened to lock this ok so this was about the recursion let's go for the loop example so this is your loop example this is fairly simple there is no recursion so I don't have to explain it much it is like you came inside this main and you have this recursive mutex M 1 similarly how you saw this before and in one loop you are trying to lock it 5 times 0 to 5 5 times and you are unlocking it 5 times so you can do it and loop also it's just a demonstration I don't have to explain much here because it is very easy I just wanted to show you that this recursive matrix can be used in loops but remember that how many times you logged it you have to unlock it that number of times so I think I'm done here thanks for watching guys and don't forget to hit the like button dude it will help me a lot and motivate me to create more videos like this I'll see in the next videos bye bye

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

YouTube Video yCYU2k77E4A Transcript | YouTubeTranscriptFree