[Music] hello friend this is rotational watching synchronous video series on C++ threading series and this topic is about timed mutex in C++ ready so before this video we'll learn about mutex race condition critical section before in this video I explain what is mutex and all the things related to that and we know if there is some variable m dot log then this is a mutex then you have to lock it and then m dot unlock this time music is also similar but it gives you the timing how long it will wait for this particular mutex what I mean to say is let's suppose there are two threads t1 and t2 ok and there is one mutex m1 dot lock you have this m dot lock here so either I am one really able to lock this first or t2 sorry t1 will able to log this or t2 either of this will be able to lock em one first and let's suppose t1 have logged then t2 have to wait for this mutex M correct so somewhere after some code you have to write m1 dot unlock then only t2 can acquire this lock till then t2 will wait here let's suppose there are few lines of the code so this was the typical behavior in mutex now can you see that if m1 is logged and let's suppose this time period is taken by p1 to actually unlock it ok unless suppose this time is 1 minute then p2 will wait here for one minute and it is kind of a time waste if t2 have something else to do but you will say that then there is something called try lock in mutex correct so the syntax would be like instead of lock we will write here Trilok then t2 will not wait for that lock and it will do something else correct I have given that video also you can see that after this video okay in the list that is called tri lock on mutex but the problem with that Trilok is it will immediately return false what I mean to say is t1 acquired the lock then t2 also tried to acquire the lock and we are using try lock then t2 will get false in that case and immediately start doing something else but I want to wait for this mutex for a particular amount of time let's say one second or 100 milliseconds and then I want to do something else not right at the moment then we have this timed mutex you will tell the time out period that I want to wait for this a much amount of time for this mutex and if I don't get that mutex in that particular time then I am ready to do something else okay so this is what the definition says standard timed of mutex is blocked till time out or the lock is acquired and returns true if success otherwise false so there are two things if this is timed mutex this one then in that case the another thread which is actually waiting for this mutex for this particular amount of time will actually get the mutex in given time let's suppose if this mutex I mean this full work is taking one minute and this t2 is ready to wait for maybe 1.5 minutes or two minutes then in that case t2 will get the lock because it will wait for this t1 to finish okay because t1 took 1 minute and t2 is ready to wait for two minutes okay then in that case t2 will get the lock and t2 will also come here and do its job but let's suppose t1 was ready to wait for only half of the minute but t even will take one minute to finish this then in that case t2 will not get the lock t2 will get false in return until you will understand that no I'm not going to get this you types in given time what is half-time okay so I'll tell you the sin Texas and all that so I have this full program here this was just the introduction and the glance what we will go through in this video ok so these are the member function what it supports there is this traditional mutex lock try lock these two function and this one is exactly similar in mutex and this but there are two different functions this one and this one so we will be covering these two functions in this video so we'll take trial out for first okay so the definition for this says that waits until a specified amount of time duration has elapsed our lock is acquired whichever comes first will happen okay and on successful lock acquisition it returns true otherwise it returns false okay so let's look at the program and we'll understand this better so this is your program here it is fairly simple program what we have in our hand is there is global amount variable which is initialized with this zero and we are creating two threads t1 and t2 and there is this function which will become thread okay and this one and two I am passing as thread 1 and thread two okay so these are the identities for the threads correct and as we are learning trial law for this is the syntax for try log 4 it will take how many seconds we have to wait for so this is timed mutex M and we are going to try the lock for one second so let's see how this will work so t1 and t2 will get created at the same time and they both will try to lock this M because it is global so this is common between these two threads okay so they both will try to lock this mutex and any one of them will succeed okay so for this moment let's assume t1 succeeded first then it is saying that try lock for this much second which is one second but as it was an unlocked position initially Tevan will immediately get the lock instead of waiting for one second it will directly get the lock because it was unlocked already okay so my amount will get incremented by 1 which is 0 so it will become 1 and I'm telling this thread to sleep for 2 seconds so what happened t1 acquired the lock mutex and it is sleeping for 2 seconds till now t2 was actually waiting it didn't do anything and then I'm printing this thread number one or two whatever has entered and then I will unlock it so that p2 can lock okay now as you know that t2 was also trying to log this at the same time but P even got the raise then t2 said hmm okay I can wait for one second if you can give me this lock in one second but what happened actually this t1 took two seconds at least I am NOT calculating how much how much time it took to increment this and to print this and to unlock it considering these two things very minimal I will say that p1 took 2 seconds and p1 was sorry t1 took 2 seconds and t2 was ready to wait for one second so as you can see that p1 actually took more time then t2 is ready to wait then what will happen for t2 it will return false here and t2 will not go inside this t2 will come here actually interior will print thread number 2 or something could not enter and lastly we are printing my amount so as t1 was entered here it will only increment by 1 so you will get only 1 as an output so let us quickly see the result and then analyze something else there is no error and if I will execute this see it is saying that thread 2 could not and thread one entered and the output is 1 so this is printing 1 and this is printing that thread 2 could not enter and did you notice thread 2 message was printed before this and that shows that thread 2 actually waited for one second only now less increment this by 2 second and make this one second yes now it will come okay so let's recompile this and execute this see thread 2 and thread one booth entered and now you have to as an output inside this are you getting this so this time what is happening let's suppose T 1 got the log first then T 2 said ok I'm ready to wait for 2 seconds and T 1 came inside this T 1 actually did something for one second and T 2 sorry T 1 unlocked it and as soon as this got unlocked T 2 was actually ready to wait for 2 seconds but it got the lock in 1 second only because T 1 got it work done in one second that's why you can see that T 1 and T 2 both was able to enter inside this so that's why this timed mutex is so beautiful if you have time constraints in your program I hope you would have understood this so let's quickly move towards example number 2 which is 4 Trilok until this is exactly similar to what we did before but instead of only simple seconds we will give the reference that you wait from now plus 1 second so our STD chrono steady clock will calculate what is current time from current time it is telling that weight plus 1 seconds ok and it will do the same job ok so let's compile this and execute this see it will tell you the same thing thread 1 was able to enter but thread 2 could not because here we are waiting for one side I mean the two was waiting for one second and the one took two seconds so actually time out came and t2 came here okay now let's do the same thing if you will make this one one and this one two then both thread should be able to go inside this correct let's see that see thread one and threat to both was able to go inside this and result is two okay so this is how time mutex work so thanks for watching guys if you liked the video don't forget to hit the like button root it will help me a lot to make more videos like this and if you have any comment let me know in the comment section I'll be very happy to answer you for those things bye bye I'll see you in the next videos
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