Menu Close

Go – AddInt32 Function in sync/atomic package in Go

In this article, we will explore the AddInt32 Function in sync/atomic package in Go in details, along with examples.

Introduction:

Concurrency is one of the most important aspects of modern programming languages, and Go is no exception. The atomic package in Go offers low-level atomic operations that can be used to build higher-level synchronization mechanisms, such as mutexes and channels. One of these operations is the AddInt32 function, which provides a fast and safe way to perform an addition on a 32-bit integer in a concurrent environment. In this article, we will explore the AddInt32 function in detail and learn how to use it effectively through examples.

What is AddInt32 Function ?

AddInt32 is a function provided by the sync/atomic package in Go. It performs an atomic addition operation on a 32-bit integer, ensuring that the operation is safe to use in a concurrent environment without the need for additional synchronization mechanisms. The function’s signature is as follows:

Syntax:

func AddInt32(addr *int32, delta int32) (new int32)

Here, ‘addr’ is a pointer to the int32 variable that needs to be updated, and ‘delta’ is the value to be added. The function returns the new value after the addition operation.

Why use AddInt32 ?

In a concurrent environment, multiple goroutines might try to access and modify shared data at the same time, leading to race conditions and unpredictable results. The atomic functions, including AddInt32, ensure that such operations are executed atomically, preventing race conditions and providing thread safety.

Example

Let’s consider a simple example to understand the usage of the AddInt32 function. In this example, we will increment a shared counter using multiple goroutines.

package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var counter int32
var wg sync.WaitGroup

func main() {

	wg.Add(1000)

	for i := 0; i < 1000; i++ {
		go increment()
	}

	wg.Wait()

	fmt.Println("Counter:", counter)

}


func increment() {

	defer wg.Done()

	atomic.AddInt32(&counter, 1)

}

In the example above, we have a shared counter variable of type int32. We spawn 1000 goroutines, each calling the ‘increment’ function, which uses AddInt32 to increment the counter atomically. The ‘sync.WaitGroup’ ensures that the main goroutine waits for all the spawned goroutines to finish before printing the final counter value.

Here, the AddInt32 function ensures that the counter is incremented safely without any race conditions, and we get the correct output:

Output:

Counter: 1000

Conclusion

The AddInt32 function of the atomic package in Go is a powerful tool to perform atomic addition operations on 32-bit integers, ensuring thread safety and preventing race conditions in concurrent environments. By understanding its usage and incorporating it into your code, you can build more robust and efficient concurrent programs in Go.

To check more Go related articles. Pls click given below link:

https://techieindoor.com/category/leetcode/

https://pkg.go.dev/net/[email protected]

Posted in golang, packages, sync

Leave a Reply

Your email address will not be published. Required fields are marked *