Golang Concurrency - Signal & Broadcast

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so ninjas welcome back to the golang dojo this channel is all about the girl programming language if that's something you fancy make sure to give that subscribe button a nice hello top and in today's video we are still on our concurrency series and more specifically we are going to be talking about broadcasting and signaling using con in the same package in the guild now if we go to the implementation of khan we will be able to see more of a description of what khan is all about khan implements a condition available for go routines to be waiting for the occurrence of an event each of the kinds has a locker very often it's a mutex which must be held when changing the condition and when the weight method is being called after which point we will be waiting for either the signal method or the broadcast method to be called in order to continue executing other instructions signal broadcast and weight are going to be the three methods that i will be talking about in detail in today's video now in the ninja verse that we have a brand new mission that we need to complete and the following instructions are going to be describing what the scenarios are now for this mission we need to take some time in order to get ready for the mission first of all we have a ready global variable at a package level indicating that whether the ninja is now ready for the mission and inside of this uh getting ready mission we will go ahead and spawn a new go routine which will be the ninja to do the task of getting ready so if we go to the get ready method it will take some time to get ready in this sleep method and there's a sleep method there's nothing more than sleeping a arbitrary number of a seconds in the range of one to five seconds after which we will say that hey we will update the ready variable to true saying that the ninja is now ready for the mission now that we are back into the main goal routine this is the second ago within the respond and now we are back to the main goal routine we will declare a integer variable uh how many work intervals do we have to do in order for this following for loop to check the condition whether the ninja is now ready or not and then we're going to print out whether we are ready and if already after how many intervals are we ready from this for loop right here now if we go ahead and run this program now it shouldn't be a surprise that it is going to take a one to five seconds in order to do the task of getting ready and doing those however many seconds that that it takes as far as it to get ready we are constantly checking the variable of a ready and while it's not ready we will have to go through so many work intervals in order for us to check if the ninja is now ready for the mission as you can probably already guess this isn't ideal because the for loop will exhaust all of the resources for this single go routine in order to just perform a very simple ready check now one very brute force way to decrease at the work interval here is to simply do is i sleep here for five seconds because we know for a fact that in order to get ready it takes us one to five seconds so let's go ahead and do this and run this program again wait for about five seconds and then we would be able to see that we are now ready after only one single work interval however we still have a one major problem that is it takes us about a one to five seconds to get ready for the mission but every single time we are spending five seconds in order to check the simple variable of ready what if the ninja only takes like one seconds in order to get ready then we are essentially wasting four seconds in order to perform a wedding check and it would be a hundred percent correct to conclude that neither approach that we've just talked about is optimal for this situation this use case right here and wouldn't it be nice if we can have the second goal scene that we've spawned and to signal the main goal routine that hey we just finished the task of getting ready for the mission and now you can go ahead and perform the check instead of having to either spend resources or time in order to check if there's a ready variable for that we will go ahead and copy this method paste it here and modify the function name to be with a coin and use con in this function for signaling afterwards we need to declare a variable which will call the sync package new con and pass in the address of a mutex it created here and we also need to go to the implementation of this getting ready function and make a copy of this we name the function to be with con and also take in the address of the content that we are about to pass in and inside of this function after we've said the ready to be true we can do a con signal and making sure that we are calling this function from our getting ready for mission with khan function with khan here and then passing the address of a con just like this except that we don't need to am percent because the new con is returning the address of the con object afterwards we need to lock the object and also make sure to unlock it after the for loop check for ready and the one final step is to actually wait for the other go routine to finish the task of getting ready with khan and setting the ready to be true and waiting for this signal to be sent from the second go routine while we are waiting in the main goal routine and also it's time to remove this line of instruction of sleeping for 5 seconds because that is the whole point of using con if we go ahead and run this program but with khan we would be able to see that we are now ready after only one single work interval instead of the last example that we had for getting ready for mission without coin where we either have to go through a large number of work intervals or having to specify a length of a time before checking the variable of ready now notice that we have a login and unlocking mechanism here and if we go to the method of aways we will be able to see that we are actually unlocking well before that we are calling this method of a runtime notified list ad and if we go to that we will see that we need to see the implementation of a sigma dot go right here and if we go to the official documentation sigma.go says this is a sigma 4 implementation that exposes to go to provide a sleep and wake up a primitive that can be used in contented a case of other synchronization primitives such as a mutex and it targets at the same goal as a linux or futex and a few tags is for all intensive purposes it's a slightly different implementation of a synchronized primitive similar to a mutex now to further simplify everything here if we go to where this function is being called in connor dot go i like to think of it as here we are releasing the old log right after we acquire a new one and you need to lock it back up after getting notified eventually and attempts to exit the wait method here now since the old lock is locked again it is okay for us to call the unlocking method once more now if you're still kind of confused which i was at in the very beginning i just like to think of it to be quite similar to weight group so if we just would move these into instructions i just like to think of it as i am waiting on the other go routine to call the signal so that i can continue with the waiting method here and be able to print out whatever i am looking for afterwards now that is for signaling incarnate let's now move on to broadcasting in con as well now it goes without saying broadcasting is pretty similar to signaling except that you are now broadcasting into multiple hergo routines i like to think of it as a beeper we are essentially doing more of the same thing here we are having a weight group though in addition to add the three because we are going to be spawning three different gold routines we need to make sure that all of these go routines has been spawned and the task for these circle routines are finished before we will go ahead and wrap up the main goal routine right here also as you can see we are passing in a function that will be calling after we've finished getting ready for the mission and we're also passing in a beeper which is going to be an address of a con object and remember that this is returning a pointer to the con object for the new kind of method so if we go to the function of a standard by for mission inside of this standby function we can see a pretty similar list of instructions as of what we had before for getting ready right here with khan essentially in the getting ready we are placing the lock and unlocking after we've recalled the weight method for the con object so if we go back to standby for mission it's a pretty similar list of instructions so we are logging the broadcaster i called it broadcaster even though i would named it to bieber instead because it's essentially like a people right you are waiting on the people to broadcast you some message and that message is going to be sent from the main go routine after the three different routines that have been waiting for the broadcasting signal if we go back to the mission right here we will be able to see that inside of the mango routine is where we would send the broadcasting signal after all three of these echo routines are on standby also i know that this is the function that we are passing in right here to print out ninja 1 is starting in the mission which is the function that we are calling right here after the beeper is finished waiting because of the broadcasting signal being sent from the main goal routine similarly for the other two go routines as well the second goal team will print out ninja 2 is starting the mission and ninja 3 is also starting the mission as the very last step inside of the main goal team will pronounce all ninjas have started their missions now let's go ahead and run this program again and as you can see we are getting we are now already after one single work interval from the function of getting ready for a mission with a con and then we get the next function where we are broadcasting all of the signals into the standby ninjas it would say that a ninja 1 is a starting mission ninja 2 is starting mission and the ninja 3 is also starting mission and at the very end we'll come back to the go routine and state that all ninjas have started their own respective missions and there you have a signaling and broadcasting this is it for today's video if you've enjoyed today's video make sure to like the video subscribe to the channel and also get your free golang cheat sheet at the golem dojo dot com slash hc and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 8,618
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go programming, golang for beginners, sync cond, cond sync, golang cond, golang sync cond, cond golang, golang signal, golang broadcast, golang signaling, golang broadcasting, golang concurrency, golang concurrency signal and broadcast, golang concurrency cond, golang concurrency signal, golang concurrency broadcast, signal and broadcast in go, signal and broadcast cond, cond in go
Id: NIvSQCwcots
Channel Id: undefined
Length: 12min 25sec (745 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.