Updated April 12, 2023
Introduction to Golang Atomic
Atomic keyword allows us to perform synchronous operations it is a package of the go language use to manage the synchronous behavior of the language, it allows us to manage the flow of the programming so that we can handle the situation where we wanted to run one go routing at a time, we know that in go language there used a goroutine all these goroutines are work on a channel and these channels allow to send and receive from one goroutine to another so with the help of the atopic package of the go language we can ensure the synchronous flow where after completion of something only other can allow executing or simply wait for the completion of the task then allow another task to execute.
Syntax
In the below example we are showing a simple example where we are creating a variable of wait group from sync/atomic package. We can explain the below syntax in the following steps.
- In the first, we have created a variable wait group from the imported package of the sync/atomic.
- Second, we are calling a function on the waiting group, we can call there are many wait group functions. We can read more about all the available functions for the waiting group in the atomic package.
- Finally, we have called the wait function on the waiting group which will play the role of waiting for the completion of the function and then allow others to perform their task.
Please see the below syntax for understanding.
waitgroup sync.WaitGroup
Waitgroup.waitgroup-function
waitgroup.Wait()
How do Atomic keywords work in Go language?
Before discussing the working of the atomic keyword we need to understand why we need it. Suppose there various goroutine channels are there and we want to perform the synchronous operation over the routine then we can use the atomic keyword.
We can explain the working of the atomic keyword in the following points.
- To use the keyword atomic we need to use the package sync/atomic and from this, we can use the waiting group.
- We need to create a variable from the wait group like waitgroup WaitGroup and over this variable, we can call various functions of the atomic.
- For example, we can call waitgroup.add() and after this we can use the go any-function, we can call many functions between waitgroup.add and waitgroup.wait() function.
- It allows functions to execute in synchronous manners.
- You know that in the Go language we have goroutine and every routine contains some channels and each channel belongs to a main channel and if the main channel terminates then all the internal channels will also stop .
- So these channels can communicate with the help of the atomic keyword in a synchronous way.
- The main power of the atomic keyword is functions like AddInt32 ,AddInt64 , AddUnit32 and AddUnit64 and there are many other functions which solve most of the problems which we face while doing some calculations and calling synchronous.
Examples to Implement Golang Atomic
In the below we have given some of the important where we are performing the some of the important operations related to the stock keyword and its package, we can get the package atomic from the package sync/atomic and from this package we can perform the operations like synchronous adding with calling functions using goroutine which calling by go. In case if we want to perform the testing of the below examples then we can create a file with name atomic.go and copy-paste the examples on the file and run the command go run atomically.go and we can get the output of the execution of the program.
Example #1
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
)
import (
"sync/atomic"
)
import (
"sync"
)
import (
"runtime"
)
//Initialising the wait group variable for further uses
//Here we are also defining the variable val as the int32 which will be used further in the program
var (
cnt int32
waitgroup sync.WaitGroup
)
func main() {
//calling the Add on the wait group
waitgroup.Add(3)
//Creating the channel of the grouting for performing operations one after another
go inc("Ranjan")
go inc("Ajay")
go inc("Vijay")
waitgroup.Wait()
fmt.Println("The value of the counter is:", cnt)
}
func inc(student string) {
defer waitgroup.Done()
for range student {
atomic.AddInt32(&cnt, 2)
runtime.Gosched()
}
}
Output:
Example #2
Please see the below example along with the screen of the output. Importing all the important packages needed for the atomic keyword.
Code:
package main
import (
"fmt"
)
import (
"sync/atomic"
)
import (
"sync"
)
import (
"runtime"
)
//Initialising the wait group variable for further uses
//Here we are also defining the variable val as the int32 which will be used further in the program
var (
val int32
waitgroup sync.WaitGroup
)
func main() {
//calling the Add on the wait group
waitgroup.Add(3)
//Creating the channel of the grouting for performing operations one after another
go append("PHP")
go append("Python")
go append("Go")
//performing the wait operation the variable created from the atomic sync package .
waitgroup.Wait()
fmt.Println("The value of the counter is:", val)
}
func append(lang string) {
defer waitgroup.Done()
for range lang {
atomic.AddInt32(&val, 3)
runtime.Gosched()
}
}
Output:
Example #3
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
)
import (
"sync"
)
import (
"sync/atomic"
)
func main() {
var val int32
//Initialising the wait group variable for further uses
var waitgroup sync.WaitGroup
for k := 1; k < 51; k++ {
waitgroup.Add(1)
go func() {
for l := 1; l < 1001; l++ {
atomic.AddInt32(&val, 1)
}
waitgroup.Done()
}()
}
//performing the wait operation the variable created from the atomic sync package .
waitgroup.Wait()
fmt.Println("The value of the val is:", val)
}
Output:
Conclusion
From this tutorial, we learned the basics concept of the go atomic keyword and we learned about the syntax and the working of the go atomic package. We focus on some of the important examples in the go language for the atomic keyword.
Recommended Articles
This is a guide to Golang Atomic. Here we discuss an introduction to Golang Atomic, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –