Practical example for pthread_mutex_trylock

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i want to go over a bit in detail and exemplify why try lock is useful and where you can actually use it first things first let's let's think about a very simple problem we have let's say we have a stove and that stove is being used by a lot of chefs right and that stove actually has fuel right has let's say gas and that gas can deplete after every every cooking so if that happens well the chefs can go home or something like that uh so in our example basically chefs chefs are gonna be equal to our friends okay the stove is really our mutex or our data our shared data plus our mutex so and the cooking uh the cooking element is literally the routine that we're gonna execute here okay so to start off we're gonna have of course uh a mutex p thread mutex t let's say stove mutex and then i'm gonna see here uh in stove fuel and this guy let's say starts with 100 and i created here the code for well creating and joining 10 threads i'm going to also create or initialize the mutex just like so and now what i want to do is to create the actual program that's going to do this well first things first after everything we're going to have to lock that mutex so again p thread mutex lock of our stove mutex and of course unlock after we're done and in between we're simply going to do a stove fuel not stove mutex stove fuel miners equal let's say a random number let's see around percent 20 okay that should be fine uh okay might as well called as friend of time of no here i think in this time.h h okay that's simple enough we know about all this and after or like before it's done let's say it takes a while to cook let's say it takes let's do you sleep of 500 milliseconds usually it takes in uh microseconds so that's why i have here 500 000 and before continuing i think we should actually check whether we have enough uh fuel so what i'm going to do is say uh fuel needed was that and if stove fuel stove fuel minus fuel needed is less than zero then just print f no more fuel going home that's it and here i guess i should print f you are left absent d backstage n is the stove fuel if it's done doing that so like that else statement and there we go so if there's not enough fuel it's just gonna print out this and if it is then it's gonna of course uh subtract the fuel needed not another random number okay and i guess we're gonna wait after after this is happening inside inside here okay amazing now let's try to execute this if we take a look at what we get in the terminal is just very simple and straightforward we just get a vertical decreasing number that okay this time we didn't go down to zero but if we for example change this to a percent 30 we should see it go down to zero and we should see that of course some treads just uh didn't have enough fuel okay that's that's a very nice problem but it's nothing really too crazy that than what we've d we've done before but here comes the catch what if we have more than one stove if we have more than one stove then that's a lot more tricky than uh than before because well okay let's suppose we have four stove fuels right we have here an array instead of just one stove fuel and let's say all of them start at 100 just like that that also means we're gonna have to have either just one stove mutex or at least uh a stove mutex for each stove so i'm gonna say stove mutex of 204 here i guess it's stove nutex c's at that point and of course if we change this we're going to have to initialize all of them so i'm going to have to change this so that it calls featured mutex you need for all four of them i have here an int i equals zero i less than four i plus plus and then we have here a stove mutex of i and the same thing here in the destroy part so destroy stoke mutex of i okay that's fine now if we change here just everything to be stove mutex of zero and stove fuel of zero that would just mean that uh we're gonna use just one stove everybody's gonna use one stove and that's not what we want and um another thing we could do is to pass in as argument the stove that's gonna be used but that's pretty determined can't we just um let it each uh chef decide for itself like get take the first stove that is available every single time and if there's none just uh wait for a bit can't we do that well using the try lock uh function we can actually do it so what we can do is create a for loop a4 i equals zero over all of them all of the mutexes of the stoves of course and say instead of lock we're gonna say try locks i'm gonna try to lock the stove mutex at the index i right so one of these mutexes is going to be try to be locked and i'm going to say here if this is actually zero so if what we did succeeded we were able to get the lock then amazing we should do this operation and of course we're gonna unlock it after we're done and we're gonna use the stove fuel off i right in all the places and if it did succeed once it's not gonna try to cook again on the next stove okay so what i wanted to do is if it was able to lock a certain mutex if it was able to cook once it should terminate its execution so i'm gonna go ahead and say here break so i want to break out of the for loop so that it finishes its execution as you can see in this situation the first thread is going to come along and well iterate through all the mutexes it's gonna say stove mutex of zero is it locked of course it's not so it's gonna probably lock it and it's gonna enter the code it's gonna do its thing it's gonna unlock after it's done and then it's gonna break out of the for loop so it's not going to cook again okay and in the meantime if for example the second chef comes along and tries to lock this uh stove mutex off zero and says that oh no no the first thread is there the first chef is there and it's actually occupied it's actually like uh working right now so you cannot do that well then it's going to iterate again to stove mutex off one and that one's gonna be empty it's gonna be able to put its uh cooking pot on the stove and it's gonna be able to cook and it's not gonna be interrupted because it got the mutex right uh the third one is gonna check the first and second one that's gonna be like uh occupied again and so on and so forth oh i miss in the initialization here the i my bad so now if we try to launch this you will notice that well only four threads were able to do some work while the others didn't actually print anything on the screen and didn't do anything else that's because well the first four came along and locked this um these all four mutexes and then the fifth i was like i don't have any space in here so he just uh went home and just finished execution so we're gonna try to prevent that it's gonna try again after let's say half a second or something like that to do that what we can do is say if on the else branch here of this guy so if it failed to get a lock and it's the last iteration so if i is actually three right so we are on the last iteration and we should actually finish our execution uh well how about you just sleep you sleep for let's see that is 300 milliseconds and it's going to sleep for that and after it it's done sleeping is gonna try again and to make the the loop try again all i have to do is just say i equals zero again so the for loop is gonna start again at the zero in this uh in this little else branch and if i try to launch this we should notice that all of the 10 chefs were able to cook and actually we can see here uh printf no stove available yet waiting so now if i try to launch this oh and the back switch end would help yep and as you can see we got this a couple times but it did wait and it did after quite a few tries some of them did get the spot in there and were able to actually cook something in the stove so this is how you can use the trilog function when you have multiple sort of shared memories or concepts they don't even have to have actual memory right this is just a stove i added the stove fuel myself but we don't have to have it really um but if you have it and it doesn't matter which one a thread chooses but it needs to choose one and that uh in that situation the trilock really helps right so if you have a point where you have multiple mutexes they are all exactly the same it's just there are multiple of them so that more threads can do things in parallel then uh a try lock call makes sense because well without a try lock you would just be waiting on that one probably predefined mutex and that wouldn't be necessarily the most optimal way of doing things that's about it for this video i hope you got something out of it if you do have any questions leave them down comments below or on our discord server the source code is going to be found at the link below to our website where you can copy it execute it whatever due to it alright take care bye
Info
Channel: CodeVault
Views: 1,936
Rating: 5 out of 5
Keywords: codevault, c (programming language), trylock, pthread, threads, mutex
Id: UrTU7ss3LDc
Channel Id: undefined
Length: 13min 12sec (792 seconds)
Published: Sat Dec 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.