Condition variables in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i want to go over the condition variables in the ptrade api namely the pitched coned weight the pitted coin signal and the pitted coin broadcast functions now to better understand this uh scenario we're gonna really try to implement something of a gas station where we have someone that's waiting to fill their car with uh fuel and someone that's actually feeling the fuel tank itself okay so uh let's say that the fuel tank is empty and the guy that comes in and wants to fuel its car well has to wait until the fuel tank gets filled okay how how do we do that we're gonna start with just simply two threads one for the guy that's waiting to fuel its car and the other one is gonna be the fuel alert it's gonna fuel the tank itself so we're gonna have here two routines uh one of them is going to be here uh let's say fuel filling it's not gonna take any arguments and the other one is going to be i guess i should also add it even though it's not gonna take any i'm gonna pass anything there just a null uh and the second one would be the let's say the car it's waiting to be filled and those are gonna be our two functions i'm gonna create i'm gonna start creating the two threads so here for the petrol create i'm gonna have basically a for loop that goes from zero to one this time and we're gonna have a if it's the last uh iteration i'm gonna have this be the fuel filling guy and if it's the the first station i'm gonna have the the car itself so the car is gonna be created first basically and of course we should join them and now let's also create the fuel itself because the station has a fuel amount and that fuel amount should be a global variable and say in fuel i'm going to initialize it with 0 because that makes sense and since we know it's a global variable and we know that both trades are going to work with that global variable it's a time to create a mutex as usual so here we're going to have to create a mutex if we thread and it's called mutex some score t it's called just mutex or actually it's not really a good idea to call your mutex mutex because that's just gonna confuse you if you have more than one mutex i'm going to call it uh let's say mutex fuel and then we know what's all about even though we don't we do have one and i'm going to initialize it here and destroy it like so all right let's see what we're gonna actually do in here of course if i just uh printf just feeling fuel from this guy and then here to get fuel if i launch this now i should get both messages printed on the screen that's perfectly fine we have both of these running nicely now how will this work well the idea is we're gonna have a simple for loop with the fueling guide this guy is just going to be in i equals zero and let's say it's gonna fuel like five times so less than five i plus plus and in here it's just to increment you all with an amount let's say 15 this time right now because of course we are modifying fuel here not only modifying but reading and writing to it we should use the log that we have just created so of course enough mutex for mutex fuel and unlock it once we're done and suppose that uh after the fueler actually fueled one time let's say that he takes a break of like one second and that's gonna be it okay and then in here in on the car's side what we have to do is well we just have to lock the the mutex right so like we did here already everything in here except that we're not gonna add to this fuel we're gonna actually subtract an amount which is uh let's say 40 because why not so actually before executing the code i'm going to add some printf statement so that we understand what's going on so feeling uh fuel let's say percent d backstage and uh fuel like field fuel more like because it's already finished field fuel there and let's say here got fuel now left is now left whatever is in here something like that so now we see the current value every time it's changing hope so far you are with me because this is quite a complex um concept condition variables and you're gonna learn it only by using an example here i cannot just show it to you straight uh forward okay so if we try to launch this program you see something very interesting happening you see that well we got fuel but now left is negative 40. so ideally we would like to have a sort of lower limit right you you can't take fuel where there isn't um i need to dig from so we would like to take fuel only when it's well there is something to take from there at least this amount inside the tank how uh how can we do that now we can simply have a if statement here that says if fuel is higher equal than 40 then of course do uh decrease the amount of fuel and print something on the screen but if the reason then you can print something else so printf no fuel black slash n and that's it and as you can see it's all right it kind of works but the car didn't get any fuel ever even though right after it the the tank got filled with some fuel so somebody could actually get and just wait for it how do we actually wait for the fuel to be there that is the question i guess what we can do is maybe we can do a while loop instead of an if here right we can just um just create a while loop right we just have a while uh fuel is less than 40 and say i don't know if it's less than 40 let's sleep for one second i guess it can change so i'm gonna copy this message from here and just say no fuel waiting and i'm gonna remove all this and of course when this while loop is um false we're gonna actually get the fuel right and when this while loop is forced of course we have more than 40 fuel or at least 40 fuel now let's see what happens i'm going to say no fuel waiting and it's going to say no fuel waiting and it's going to keep on doing that forever and ever and ever never stopping why is that i mean shouldn't it you know fuel because we have a fuel filling guy not really the issue is really with the mutex here so inside this function right we are locking this mutex fuel so we're saying okay well nobody else that tries to lock this mutex can lock it right now okay so we lock it and then we wait and then this function comes along and says okay well let me start fueling the station and it tries to lock but it can't because it's still not unlocked this mutex is being used here so you just wait and this guy just sleeps forever because the fuel value never changes and this guy also waits forever because the mutex is never unlocked while yes we could unlock and lock it inside the while loop there is a certain concept inside the ptrad api that can help with this and that is you guessed it the condition variables so these condition variables are as they are named there are certain variables on a certain condition in our case the condition is that the fuel is less than 40. so how do these condition variables work first we can create one we have to create here a simple variable p thread called underscore t and let's say fuel or like let's say cone fuel all right and this guy has to be initialized like all the other p thread entities so p thread found in it that just takes in the uh view i think it says corn fuel yep and then some attribute as their second parameter that right now we're just gonna leave it uh at uh default so we don't have to customize it at all and then i'm gonna also call the thread point destroy and of course pond viewer now okay so that's the first step we have created a condition variable that is out here now the condition variable comes with mainly uh three operations that you can do on it why is it to wait on the condition another one is to broadcast and signal on those waiters what does that mean well here instead of uh sleep what we can do is simply call p thread count weight count weight and we'll just give it the condition the point fuel and we're also going to give it the mutex that is very important okay in this way is actually going to wait for a signal from another thread that may or may not change the condition so in order to trigger this right now if we just run the program what's gonna happen is that we're just going to see no fuel waiting once the gas station is going to get filled but nothing else so now the program is kind of stopped because the second try is just waiting right this weight is waiting for signals doesn't do anything right this is what this uh count weight does unless we actually send a signal from somewhere and that somewhere could be this thread okay so once we're done filling with fuel we can send a signal saying signal confuse and that that says to all the threads that are um waiting on these conditions all the tests that have called weight con fuel are gonna continue execution but it's important that you have this in a while of the condition itself see the condition itself is not contained in the condition variable it's contained in here in the while loop itself and it's important to have here a while loop because notice we're only adding 15. so we go from 0 to 15 and then if we signal then the fuel might not be enough right so we have to check again this signal is going to start this thread and it's going to be able to possibly get fuel and be done now if i try to launch this you'll notice it's going to say no fuel for some time and then it's gonna say got fuel which is very important right let me actually make this bigger so he said no fuel waiting right it got here and the important part is the fueler field 15 so it did fill 15 here and then it signaled a signal the condition waking up this thread and making it to check again the condition says okay well is this condition false again or true again it's still true the fuel is less than 40. okay i'm going to print this on the screen which you can see here and i'm going to wait again so it's going to call code wait a second time right and then it's going to wait for the second signal from the other thread that it did in here and go to 30 and it's still no good but at one point it got to 45 and it was good this was false and because this was false it managed to actually uh subtract 40 fuel from it and print f that and just exit and get done wiping even though the gas station was still being filled right so in the middle of that you were able to check and get the fuel that you needed even though it wasn't completely done getting filled now this is nice and all but there are a few very important aspects to understand here um before going peter cond can't wait at least what it does is see you're passing in the cone variable and the mutex and this mutex is actually going to be unlocked so when you call this it's going to actually call peter mutex unlock of mutex fuel and it's gonna wait so that's sort of kind of what it does it just just calls this let's say equivalent to this then wait for signal on horn fuel and then when it waited it's gonna call petrol mutex lock again okay so that is how when even though we're in a while loop and we have locked the mutex we were able to fill the gas station that is how this all was able to work the con signal didn't do anything with the mutex it left it as it is as you can see we don't even pass this mutex here it just tells all the waiting threads that called weight on this condition that they could continue but the weight call does all these three things and it's very important for you to understand this concept so a quick recap a conditional variable is an identifier for a certain signal that you could either well signal or wait on right on signaling your signal not that the condition is true you're just signaling that the condition might the the conditions result might have changed so that well you see the first time i signal this a signal with fuel 15 of course this is going to continue execution but the condition is not uh false yet so we're still in the while loop okay so we're going to continue on doing that until it actually is more than 40. and the weight on condition does unlock the mutex right so that's called in its implementation just called the mutex unlock so another thread could come in and wait for fuel as well without having any issues uh regarding a mutex lock now of course don't forget to initialize the condition first that is very important i know i just skimmed through this but it's very important that you initialize both the mutex and the condition variable and you destroy it afterwards that is not as important but i just do it um that is about it for this video i'm gonna actually break it up into two parts because it's already getting quite long so the second video are gonna take a look at that broadcast that i was talking about and a few other concepts regarding condition variables because as you can see it's quite a complex concept but i hope you got something out of this video if you do have any questions please do leave them down in comments below or on our discord server the source code you're gonna find on the website uh at the link in the description below thank you so much for watching take care bye you
Info
Channel: CodeVault
Views: 14,259
Rating: 4.9935794 out of 5
Keywords: codevault, c (programming language), condition variables, condition vars, cond, vars, variables, pthread, thread
Id: 0sVGnxg6Z3k
Channel Id: undefined
Length: 18min 57sec (1137 seconds)
Published: Fri Dec 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.