Golang Atomic

Introduced to Golang Atomic

Atomic keyword allows us to perform synchronous business it is a package of the see lingo use to manage the synchronous demeanor 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 run communication there used a goroutine all these goroutines are work on a channel and these directs allow to send and receive from one goroutine to another so with the assistance of the atopic packet of the return lingo we can ensure the synchronous spurt where after completion of something only other can allow executing or simply wait for the completion of the task then stand another task to execute.

Syntax 😛 TAGEND

In the below example we are showing a simple example where we are creating a variable of wait radical from sync/ atomic package. We can explain the below syntax in the following steps.

In the first, “weve created” a variable wait group from the imported package of the sync/ atomic. Second, we are calling a function on the waiting radical, we can call there are many wait group performs. We can speak more about all the accessible offices for the waiting radical in the atomic container. Finally, we have called the wait function on the waiting group which will dally 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 many goroutine paths 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 numerous functions of the atomic. For example we can call waitgroup.add() and after this we can use the lead any-function, we can call countless offices between waitgroup.add and waitgroup.wait() office. It stands runs to execute in synchronous styles. You know that in the Go language we have goroutine and every procedure contains some paths and each direct belonging to a prime path and if the primary channel completes then all the internal canals will also stop. So these channels can communicate with the help of the atomic keyword in a synchronous acces. The primary power of the atomic keyword is gatherings like AddInt3 2, AddInt6 4, AddUnit3 2 and AddUnit6 4 and there are many other functions which solve most of the problems which we face while doing some calculations and calling synchronous.

Illustrations to Implement Golang Atomic

In the below we have given some of the important where we are performing the some of its most important actions related to the stock keyword and its pack, we can get the package atomic from the pack sync/ atomic and from this package we can perform the operations like synchronous adding with announcing gatherings consuming goroutine which calling by seek. In case if we want to perform the testing of the below lessons then we can create a file with honour atomic.go and copy-paste the instances on the document and move the bid move run atomically.go and we can get the output of the execution of the program.

Pattern# 1

Please see the below instance together with the screen of the output.

Code 😛 TAGEND

package prime import( “fmt”) importation( “sync/atomic”) importation( “sync”) import( “runtime”) //Initialising the wait radical variable for the purposes of the utilizes //Here we are also defining the variable val as the int3 2 which will be used further in the programme var( cnt int3 2 waitgroup sync.WaitGroup) func main() //calling the Add on the wait group waitgroup.Add(3) // Creating the direct of the grouting for performing functionings one after the other go inc(“Ranjan”) go inc( “Ajay”) go inc(“Vijay”) waitgroup.Wait() fmt.Println(“The value of the bar is: “, cnt) func inc( student string) defer waitgroup.Done() for stray student atomic.AddInt32(&cnt, 2) runtime.Gosched()

Output 😛 TAGEND

Golang Atomic - 1

Example# 2

Please see the below illustration together with the screen of the output. Importing all the important cartons required in the atomic keyword.

Code 😛 TAGEND

package main importation( “fmt”) import( “sync/atomic”) importation( “sync”) import( “runtime”) //Initialising the wait group variable for the purposes of the utilizations //Here we are also defining the variable val as the int3 2 which will be used further in the programme var( val int3 2 waitgroup sync.WaitGroup) func prime() //calling the Add on the wait group waitgroup.Add(3) // Creating the direct of the grouting for play-act functionings one after another go append( “PHP”) make append( “Python”) rise append( “Go”) //performing the wait running the variable made from the atomic sync package. waitgroup.Wait() fmt.Println( “The value of the bar is: “, val) func append( lang fibre) defer waitgroup.Done() for scope lang atomic.AddInt32(&val, 3) runtime.Gosched()

Output 😛 TAGEND

The value of the counter

Example# 3

Please see the below precedent along with the screen of the output.

Code 😛 TAGEND

package main import( “fmt”) import( “sync”) importation( “sync/atomic”) func main() var val int3 2 //Initialising the wait group variable for the purposes of the employs var waitgroup sync.WaitGroup for k := 1; k< 51; k ++ waitgroup.Add(1) move func() for l := 1; liter< 1001; liter ++ atomic.AddInt32(&val, 1) waitgroup.Done() () //performing the wait enterprise the variable originated from the atomic sync pack. waitgroup.Wait() fmt.Println( "The value of the val is: ", val)

Output 😛 TAGEND

The value of the counter

Conclusion

From this tutorial, we learned the basics conception of the go atomic keyword and we learned about the syntax and the working of the go atomic package. We places great importance on some of its most important samples in the become lingo for the atomic keyword.

Recommended Essays

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 –

How to Install Go ? Algorithm in Programming Best Programming Languages Scratch Programming Examples

The post Golang Atomic showed first on EDUCBA.

Read more: educba.com