Learn Golang Concurrency in 15 Minutes | Golang Concurrency Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video you're going to have full understanding of the concurrency basics in the go programming language we'll be doing a speed run on the things like go routines and channels as well as many of the essential components in the sink package in go in only a few minutes without any fluffs so let's get this running [Music] some ninjas welcome back to the golang dojo hit the fastest and going channel for holy ghost programming language and make sure to subscribe to this channel and pick up your free yes free hey go line q sheets at the golangdojo.com now you should know that all of the components i'll be talking about in today's video i have a full length tutorials on each one of these so if you want more granular explanations on these components make sure to check out this a playlist in the corner right here we have a ton to cover today in a short amount of time so let's get right into it first we have a go routines which are pretty similar to threads in most other languages all we need is the keyword to go follow her by a function which is going to have the task to perform on this new thread or go routine we go ahead and print out new go routine right here and after that we will go ahead and print out this is the old one go ahead and run this program as you can see we have a new go routine and this is the old one put it out in a non-deterministic order right here next we have a channels which are pretty unique to go as a means of a communication between a go routine to create a channel we will use the make method in go and pass in the channel and the type of the channel when we spawn a new routine we can now use the channel to communicate this string message let's say that we want to communicate a string of hello world like this after that we can go ahead and print out there's a message that we've passed in in a different nego routine in our main goal routine by using the arrow on the left instead of on the right go ahead and run this program again as you can see we are seeing hello world being printed out using the main goal routine by default the channels have zero buffers meaning that if you pass in a message you have to get the message immediately back out otherwise we are going to have a deadlock or we can use a buffered channel which can be initialized with a make function again passing in the channel and the type of the channel and then the size of the buffer now we can pass in multiple messages into this channel without having head downloads like this now let's go ahead and print out the messages coming out of the channel again go ahead and run this program as you can see we are seeing her one and two her being printed out being extracted out of the channel and if we don't we assign the channel with a buffer of a size 2 as i comment this out run this program again as you can see we are going to get a deadlock there are a lot more about channels that i can cover in today's video so make sure to check out the channel's videos in my playlist coming up next we will start talking about the components in the single package and more specifically let's start with weight groups with weight groups that we can have a one go routine to wait on another go routine to prevent race and conditions for that we will go ahead and initialize a variable of a weight group like this and add in the number of girl routines that we want to wait under the main ego routine and spawn the newer gold routines that we are going to wait with the main nego routine so let's go ahead and have this enugo routine to print out again new her go routine like this and don't forget to call done so that this following awaits a method will be notified and then the following instructions will be called so we will go ahead and print out this is the old one after there's a new go routine here it has finished executing and called done go ahead and run this program again as you can see we are getting a new go routine and this is the old one except that remember last time the order is non-deterministic but we now are waiting for the new go routine to finish first if and only if then we would go ahead and print out this is the old one and this helps us with the ordering of the go routines as well the next component in the sync package we have in mutex which helps with mutual exclusion on the resources now let's say that we have a thousand iterations for this for looper right here in each of the iterations we wanted to spawn a new girl routine and each one of the guru team is going to increment this sum integer variable starting from zero also helping out the value of sum afterwards go ahead and run this program as you can see we are actually getting something less than a thousand even though we have a thousand iterations in a thousand and go routines y here now this could be caused by not every single go routine has been spawned before we print out this sum right here so let's go ahead and utilize the weights group that we've talked about previously yeah the number of iterations to the weight group and call her done in each one of the guild routines after incrementation also make sure to wait so that the mango routine will be the last ego routine that finishes executing go ahead and run this program again unfortunately we are still getting a number a result that is less than the number of iterations which is a 1 000. and that tells us that the summer variable right here is not thread safe or go routinely safe and it needs to be protected by a mutex for that we will go ahead and initialize the mutex right here and then before the incrementation we will log this mutex and unlock the mutex after the incrementation let's say go ahead and run this program again as you can see we are finally getting a thousand as the final value of a sum after a thousand incrementations next we have ones in the sync package to ensure onenet task is only performed once even though we have multiple go routines running trying to perform at the same task here we have the exact same instructions so we have we assign the summer to zero and then we are adding a thousand iterations into the weight group and we have a for loop that is going through a thousand iterations spawning a thousand goal routines and each of the routines is going to increment this summer right here however what if we only want to increment the sum exactly once and no matter how many goal routines that we've spawned we can do that using the ones in the same package we'll go ahead and initialize the variable once and i call the ones do method right here and give it a function and inside of this function is where we're going to move the actual instruction that we want to perform exactly once right here as you can see we are only going to increment this sum variable exactly once and the value is 1. next we have a pool in the sync package that helps with reutilizing the resources that we've previously allocated let's say that we want to create a memory pool we will go ahead and call the singh data pool structure and inside of this rock we will go ahead and implement the new member method which will return an interface a type we will create or allocate one megabyte of a memory and we will go ahead and return that piece of memory next we will go ahead and call the geta method which will call this a new member function right here and return the very first piece of memory that we've allocated and also make sure to cast it to the type that we want like this maybe you're going to have some instructions right here to utilize it there's a piece of memory after using this piece of memory though we can go ahead and put this right back into the memory pool so the next time we are calling the get method and there's a piece of memory has already been put back into the memory pool we don't have to perform additional allocation but we turn this a piece of memory that's been used before next we have a coin in the sync package which we can use to signal or broadcast between go routines now this is the basic structure when using con in gold now you may be thinking this is a strangely similar to weights group except that when we are calling the new content method we need to pass in the address of this and mutex that we've created just for this appointment and you'll be correct and it is it's strangely similar to her waves group because we have this signal method here which is pretty similar to the done method in a way group and more similarly we have this exact same weight method afterwards in the main goal routine after this new goal routine has finished its task not so similarly though before and after calling this a weight method we actually need to call the log method in the unlock method like this also when we are changing the conditions perhaps you have an instruction here that needs to change at some condition which will be checked in the main goal routine perhaps you're going to have like uh checking the condition right here using some type of for loop like this this would be the condition that you're checking which will be changed somewhere in a different ergo routine here we would have to call the halakka method in between the instructions that will change there's a condition that will be checked in the main go routine because this is the whole point of using con in go and being able to change the conditions safely as well as having a way to be notified in the main goal routine that you've exchanged the condition using the signal here method or using the broadcasting method like this if you have a multiple hergo routines that are waiting that you want to have notified next we have a single data map which is a threat safe or go routine safe map indigo here we have a regular map initialized and we are adding a thousand iterations to the weight group and a follower with a thousand iterations right here in each of the iterations again we are spawning a new goal routine and we are calling the done on this away group here and on the main goal routine we are waiting on a thousand of these amino routines to be done now everything seems fine and dandy until we run that this program as you can see unfortunately we are going to see some pretty dramatic errors here but more importantly you're going to see this message saying that we have a summon concurrence map right performed right here that is because the regular maps in go on safe or your routine is safe and this is where the sync mapper comes into play we can go ahead and initialize the sync map right here calling the single data map library and instead of using the regular map assignment we can go ahead and use the syncing map data store in passing the exact same thing give it a key as well as the value right here go ahead and run this program again as you can see we don't get the error on conquering a white to this sinking map like we did with the regular map finally we have atomic in the sink package let's say that we have an in n64 variable right here and we want to perform an atomic addition to this into 64 variable like this we are incrementing the i by a one right here this is the equivalent of as if we have used the mutex instead and we are doing i plus equal one right here and use the mutex again to unlock the resource of i available now as you can probably already guess as well this is isn't the only advantage of using atomic it'll be pretty silly to just save a couple lines of instruction and create a whole brand new component in the sync package right because there is some performance advantage using the atomic package in go as well for more detailed benchmarks and make sure to check out my last video that i did on atomic in the sync package in go on top of that of course we aren't limited to the types that we have a method for in the aptel maker package we can also utilize the atomic data value in order to perform atomic operations on a custom types for example let's say that we have a custom type of a ninja right here which is a struct and it has a member variable of name which is of a type string we would be able to atomically store different ninja objects like this as well as getting the value that's a store using the loh method that's it for today's video on the concurrency basics in the go programming language again if you want to have a more granular explanations on each one of these components that i've talked about in today's video make sure to check out this playlist right here for the time being make sure to give this video a like and subscribe to the channel and pick up your free hey golang cheat sheet at golangdojo.com and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 1,941
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go programming, golang concurrency, golang concurrency tutorial, golang concurrency crash course, golang concurrency course, golang concurrency in 15 minutes, learn golang concurrency in 15 minutes, golang concurrencycrash course, concurrency in go, go concurrency, go crash course, golang crash course, concurrency in golang, go concurrency crash course, golang concurrency basics
Id: _uk9BN3a0eo
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Fri Oct 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.