Signaling for condition variables (pthread_cond_signal vs pthread_cond_broadcast)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's continue where we left off with the gas station uh little implementation where we had two threads one of them fueling the gas station and the other one taking fuel from that gas station using the uh condition variables that we uh learned about now this works fine if we only have one thread of each so this is perfectly okay we can see that if we launch this we're just gonna get uh the proper waiting time and then after there's enough fuel the car actually decides to get a cue the fuel from the gas station and then it gets filled again um but what happens if we have more than one car at the gas station let's say we have four cars what's actually gonna happen um if we try to change i'm gonna change this here so that it's a five so this one this one and this one is going to be all five all right so we're gonna have one uh guided fuels the gas station which is gonna be the last one created actually there's four and the other four are gonna be the actual cards and here of course going to join all of them let's see what's going to happen if you try to run this you'll notice something very interesting so first thing we can notice is that well there just isn't enough fuel for all the cars and the program just hangs there waiting for the fuel to come but there's no fuel to be fueled with so that's one issue but the other issue is something that we can notice that whenever with fuel only at most two cars get awakened from their uh slumber to check if they have fuel right so here you see i getting 15 fuel there are two cars that check and then getting 30 fuel there's only one install 45 again one that checks and gets actually the fuel and then again there isn't uh enough and so on and so forth and then when we get to the end there of course isn't enough fuel for everybody now this is a bit interesting what did you expect that all the threads would wake up at that point so instead of like after the after we fueled 30 into the gas station wouldn't you expect that four uh cars would look and see whether or not there is a uh enough fuel for them to use well that's what i would expect but this is not what happens here and that is actually because really the behavior of petered corn signal so petered corn signal in this case um all it does is it signals one thread to wake up and actually check whether or not it has enough fuel and if it doesn't that's it it doesn't go to the next car and checks if if it doesn't it's gonna wait again and that's it so this uh implementation currently just signals one of the threads at basically at random tries to have priority and it's gonna be based on that uh but in this case all of them have the same priority so it's gonna signal only one thread and only one trade going to check that makes the program not necessarily correct now one thing that is important to realize is that well you can never execute this code here where it checks in the while loop in a multi-trans fashion because if you're executing this code this condition that means that you get the you got the lock on that mutex and that means that no other thread got that lock so it's of course going to be all the other tests are going to be waiting on that lock and only one thread is going to be able to check and of course if it's going to be able to check it's going to be able to subtract if it can okay so in this situation it kind of is correct because really if there is for example 45 fuel right so if there's 45 fuel here uh then well one thread is going to be signaled and it's going to say okay well we have more than 40. so we can get out of the while loop we're going to subtract we're going to print the message and then we're going to unlock right and then the fuel is going to be five so at that point right after unlocking there's no point in actually uh checking again if there is enough fuel because we know for sure that there isn't enough fuel if the if the thread actually was able to subtract fuel from it but this is not always the case it might be that instead of fueling 15 at a time we might be feeling 80 or 60 or something much larger that would cause this situation so let's say we actually fuel 60 at a time right so 60 times 5 that's 300 and we here are expecting to have only 160 in total so now if i try to launch this we can see that at every point we do get a thread that finishes execution but at some point namely here where we have 80 two threads could have two cars could have uh filled their fuel tank with 40 fuel right because 2 times 40 is 80. but only one did and after it it started the fuel alert started fueling again because only one thread got awakened using the pitch signal right so there are multiple threads waiting on this condition like we have here with signal only one of them gets awakened so what can we do how can we make it so that all of them do awaken because at this point we can have situations where we want to have that where we want to uh for two cars to actually fuel their fuel tank well we can use instead of pitted cone signal we can use petrol called broadcast and this guy is exactly as it says it broadcasts the signal to all the waiting threads so now we're going to see a whole different uh result if we try to launch this you will notice that every time a a field fuel message occurs we get at least four messages on the screen so here we got a gold fuel and other three well looked at the fuel it says okay well this guy got uh that from 60 this guy subtracted it to 4 to 20 and then well i now have 20 so not enough for this condition therefore it's just going to be waiting again waiting again and waiting again again don't forget these are actually sequential even though are in different threads because whenever each of the thread executes this code they have a mutex locked right so this one got executed first then this one then this one and then this one none of them got executed at the same time okay so we now have four of them after each fuel fueling message right so we can see that okay here four but after it we only get three because well this guy actually got the fuel and finished its execution so now we have three here so this stage we have three two of them got the fuel so that is the expected result this is what i was expecting to see and it's actually improved an improvement we got two of them to refuel their tank and then uh one last thread that had to wait one more round of fueling so this is what petercon the broadcast does now it's not guaranteed with petercon signal that only and only one tread is awoken but it is not at all guaranteed that all of them are going to be open so for uh all kinds of purposes you should actually uh use broadcast when you you want all the threads that are waiting on that condition to awaken to restart executing and check the condition and at this point we can change it to have as many fuelers as we want so if we let's change this to let's say 30 and we're gonna go here in the main for loop let's say we can say six uh actually i have to change this to six and this one to six as well and i say i equals four or i equals five and then i'm going to have two fuelers and four cars that are waiting to fuel their gas tank so now if i try to launch this of course i'm gonna get uh two times those messages where it says field fuel but at every point it says field fuel that broadcast occurs for all the waiting trends okay so uh here in the beginning we might be getting just two messages simply because remember we are still having to lock the mutex in the beginning so uh it might be that two cars got to lock this mutex and get to the picon pit con weight which again unlocked it but the other two didn't get to do this so they are stuck here for a bit right and then probably this is where the other two started or got to this pitcher can't wait but after it after that the whole endeavor finished like after all four threads got past this lock you can see that after each fuel we have four messages and of course we have three two one and then that's it and this is really all there is to condition variables for the most part there are certain attributes that you can add to the uh condition variables that we might go over at a later time but for most situations this is enough and i hope you understood what's going on here and they're going to be able to use the condition variables wherever you need to and if you do have any questions leave them down comments below or on our discord server the source code for this lesson can be found on our website again link in the description below take care bye
Info
Channel: CodeVault
Views: 5,735
Rating: 5 out of 5
Keywords: codevault, c (programming language), pthread, condition variables, signal, broadcast
Id: RtTlIvnBw10
Channel Id: undefined
Length: 11min 23sec (683 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.