Related Articles

Related Articles

atomic.StoreUintptr() Function in Golang With Examples
  • Last Updated : 01 Apr, 2020

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The StoreUintptr() function in Go language is used to atomically store val into *addr. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.

Syntax:

func StoreUintptr(addr *uintptr, val uintptr)

Here, addr indicates address.

Note: (*uintptr) is the pointer to a uintptr value. And uintptr is an integer type that is large too large that it can contain hold the bit pattern of any pointer.

Return Value: It stores the val into *addr and then can be returned when required.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// StoreUintptr function in Golang
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Defining variables for 
    // the address to store the val
    var (
        x uintptr
        y uintptr
    )
  
    // Using StoreUintptr method
    // with its parameters
    atomic.StoreUintptr(&x, 444443)
    atomic.StoreUintptr(&y, 223)
  
    // Displays the value stored in addr
    fmt.Println(atomic.LoadUintptr(&x))
    fmt.Println(atomic.LoadUintptr(&y))
}

chevron_right


Output:

444443
223

Here, first, the uintptr value is stored in the addresses defined then they are returned using the LoadUintptr() method above.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// StoreUintptr function in Golang
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Defining variables for 
    // the address to store the val
    var (
        x uintptr
    )
  
    // Using StoreUintptr method
    // with its parameters
    atomic.StoreUintptr(&x, 5255151111)
  
    // Loading the stored val
    z := atomic.LoadUintptr(&x)
  
    // Prints true if values
    // are same else false
    fmt.Println(z == x)
  
    // Prints true if addresses 
    // are same else false
    fmt.Println(&z == &x)
}

chevron_right


Output:

true
false

Here, the value stored and loaded are the same so true is returned but their addresses are not the same so false is returned in that case.




My Personal Notes arrow_drop_up
Recommended Articles
Page :