Menu Close

Go – StoreUintptr() in sync/atomic package

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

Introduction:

Concurrency is a critical aspect of modern software development, and Go is a language specifically designed to handle it efficiently. One of the essential features provided by Go is the “sync/atomic” package, which offers atomic memory operations to ensure data consistency and prevent race conditions. In this article, we will explore the StoreUintptr function, an important part of the sync/atomic package, and provide a detailed example to demonstrate its usage.

What is StoreUintptr Function ?

The StoreUintptr function is part of the “sync/atomic” package in Go, which supplies low-level atomic memory operations. This function atomically sets the value of a uintptr variable. It is a write operation that ensures the value written is immediately visible to other concurrent threads. The function signature is as follows:

Syntax:

func StoreUintptr(addr *uintptr, val uintptr)

The function takes a pointer to a uintptr variable addr and a uintptr value val as arguments. It sets the value of addr to val atomically.

Example

To illustrate the usage of the StoreUintptr function, let’s create a simple program simulating concurrent data access. We will create two goroutines: one that sets a shared uintptr variable to a specific value using the StoreUintptr function and another that reads and prints the uintptr value using the atomic.LoadUintptr function.

package main


import (

	"fmt"

	"sync"

	"sync/atomic"

	"time"

)


func main() {

	var counter uintptr = 0

	var wg sync.WaitGroup


	// Writer goroutine
	wg.Add(1)

	go func() {

		defer wg.Done()

		for i := 0; i < 5; i++ {

			atomic.StoreUintptr(&counter, uintptr(i+1))

			fmt.Printf("Writer: Counter set to %d\n", i+1)

			time.Sleep(time.Millisecond * 500)

		}

	}()


	// Reader goroutine
	wg.Add(1)

	go func() {

		defer wg.Done()

		for i := 0; i < 10; i++ {

			fmt.Printf("Reader: Current counter value is %d\n", atomic.LoadUintptr(&counter))

			time.Sleep(time.Millisecond * 250)

		}

	}()


	wg.Wait()

}

In this example, we use the sync.WaitGroup to ensure that both goroutines complete before the program exits. The writer goroutine sets the counter value using the atomic.StoreUintptr function, ensuring atomicity. Meanwhile, the reader goroutine reads the counter value using the atomic.LoadUintptr function, guaranteeing that it always reads the latest value written by the writer goroutine.

This example demonstrates how the StoreUintptr function can be used to perform atomic write operations on a shared uintptr variable, ensuring data consistency across concurrent threads.

Conclusion

The StoreUintptr function is a valuable utility provided by the sync/atomic package in Go, enabling atomic write operations on uintptr variables. It is particularly useful in concurrent programming when multiple goroutines access shared memory. Using the StoreUintptr function ensures data consistency and helps eliminate race conditions, contributing to the overall stability and reliability of your concurrent Go applications.

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

https://www.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 *