Golang Concurrency - Atomic

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] some ninjas welcome back to the golang dojo the fastest and growing channel for heavy gold programming language make sure to subscribe and get your free higgo lang chi shi's at issue golengojo.com and in today's video we'll show another concurrency series and more specifically we are talking about atomic in the sinkhole package in ego now just like the other useful tools in the single package in go while atomic can provide a low-level memory primitives that are useful for implementing synchronization algorithms we should always use it with a conscience and in today's video we're gonna talk about what is it for and in what cases you need to pay his special attention now the very first use case it would be when you have a variable let's say that it's a int 64 and you want to perform some atomic additions to this variable and all you need is actually called the atomic package and calling the add into 64 and passing the address to the variable that you've declared already and add in the delta that you want to yeah maybe you want to add one to the value of sum then you would be able to print out what sum is afterwards and also why before it because it is initialized to zero in the beginning this one should print out zero and this one should print out one go ahead and run the program as you can see here we are getting a zero and one and if you are not using the atomic package you can do the same with a simple meal tags as well so this whole block will be the equivalent of as if you have a mutex and you are using the mutex to guard against this atomic addition so you could do something like sum equals to sum plus one and you would release the mutex afterwards and go ahead and print out the value of sum afterwards let's go ahead and run this program again as you can see here we are getting zero and f1 to zero which is one and add 1 into 1 which is going to be equal to 2. of course one advantage that you can say about atomic is that you can potentially save some lines of instruction because all you need to do is calling the atomic package and calling the methods that you're trying to call instead of having to declare a mutex and then you have to perform a log and unlock operations on every single atomic operations that you want to perform however creating a standalone component in the sink package just for this if one particular advantage is pretty silly as you can probably already guess which leads us into talking about the performance benefits that atomic can potentially bring it to your program as a result i've written some benchmarks so that we can see if there's also a performance advantage for using the atomic add instead of using the mutex for the atomic operation now for the mutex out here we have a weight group instantiated and we are adding the number of iterations and inside of this for loop which will iterate through the number of iterations right here for each iteration we will have spawn a go routine again if you haven't checked out the basics of regardless to make sure to check out my last video and we will defer on the call to the method of done for the wake group so that this weight here will eventually get satisfied after each one of these atomic additions is performed and avoid the benchmark for the atomic add it is almost the exact same instructions except instead of using the mutex here we are calling the atomic package and calling the method of add integer 64 and then passing the address of the variable sum here and then also giving it the delta now let's go ahead and run the benchmarks go test bench equals dot and let's wait a little bit as you can see we have a pretty significant performance advantage with the atomic add here compared to using the mutex let's now go back here to the main.go file and go to the implementation for the add in 64. and as you can see there are other methods available alongside with added into 64. we have at ins 32 and unsign in 32 along with many others and also we have the again operation and the pull the operation with load and soar respectively for load it will take in the address and load whatever value is within that address and if we do a store methods it will store whatever value that you are passing in and store this value into the address of the variable that you've provided so let's say that you have a different some here which is also of a type of 62. you can now call the atomic package and also call the loader method in order to get the value of the variable div sum here we can go ahead and print out what the value is which should be zero here and we can also call the atomic and do the store in c62 and pass into the address of a different sum and also the value that we want you want to store into the different sum here let's just say one and then we will go ahead and print out the different sum again let's go ahead and run this program and as you can see the last two values here we are getting zero because it is initialized to zero by default and we are getting one because we are changing the value for this variable that we've passed in to the value that we are passing in here and i print out the div which is going to be the the different sum that we are putting out right here now you might be thinking at this point this is all fine and dandy but it seems like there's a very limited list of types that we can use the atomic background for example we can yeah do like n64 unsigning set 32 but it's still a very limited set of types that we can utilize and you'll be percent correct it doesn't come with some unlimited list of uh operations or types that we can perform the atomic operations on and that is why we have atomic data value which will allow us to do the load and the store with whatever custom types that we choose to create let's say that we have a ninja type right here and we can pass in a ninja object just like this and so he'll be able to perform atomic concurrent operations on to this object for example let's say that we actually have a member variable inside of this ninja struct right here and that is the name variable and also let's uh remove these two lines right here and call this atomic value av instead now we will go ahead and store a ninja right here but before that we need to instantiate there's a ninja we will call it wallace yours truly and they give it a name and this ninja's name is nothing other than wallace we will go ahead and store this wallet variable into this av holder right here next up we will go ahead and spawn a new go routine and inside of this going to perform some operations on it to the av right here but before that we need to instantiate a weight group so that we can take care of any of the razor condition or unwanted waist condition to add because we are only going to spawn one and go routine to perform operations or modifications into the av right here inside of this go routine we will go ahead and try to get the wallet or the object to store into the av by calling it the loher method and also casting it as a ninja and we will go ahead and modify the name of this ninja to nod wallace and store it right back in like this and we will go ahead and call it done here for the way group and also we need to wait so that we will not issue any waste conditions and at the very last we will go ahead and print out whatever we have in this av variable by calling the low method and casting it as a ninja and also calling its name as well let's go ahead and run this program again and as you can see we are getting nod wallace right here because even though we are storing wallace with the name of wallace we are getting the wallets right here and then we are modifying the name for there's a ninja variable to not the wallets and then store this value right back in now another thing that we need to notice is that we are not storing the address of the variable that we are passing in meaning that if we try to modify this ninja object call it wallace again and run this program again as you can see we are getting stole not a wallet instead of a wallet again because we are not passing in the address of this object but we are just passing in a copy of the object wy here and as you can see if we click on it it is a object of a type interface instead of a pointer to that interface object compared to if we go to the add integer 62 we are passing in the address of the sum variable as well as when we are storing the 1864 we are also passing in the address of this div summer variable right here so make sure to make that a distinction and that is it for today's shorter video on the atomic package in a gill if you have any questions make sure to leave them in the comments section down below and make sure to grab your free highline cheat sheet at golangdojo.com and make sure to subscribe to the channel and i'll see you ninjas in the next video [Music]
Info
Channel: Golang Dojo
Views: 1,156
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, Golang Concurrency Atomic Safety & Performance, golang concurrency, golang sync package, atomic in golang, atomic safety and performance in golang, golang atomic, sync package in go, atomic safety in golang sync package, sync package atomic in golang, golang concurrency tutorial, golang concurrency explained, golang atomic tutorial, golang sync
Id: xXi9yWbXkHU
Channel Id: undefined
Length: 10min 44sec (644 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.