atomic.LoadUintptr() Function in Golang With Examples

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The LoadUintptr() function in Go language is used to atomically load *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 LoadUintptr(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 too large that it can contain the bit pattern of any pointer.

Return value: It returns the value loaded to the *addr.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// LoadUintptr function in Golang
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Assigning values
    // to the uintptr
    var (
        i uintptr = 98
        j uintptr = 255
        k uintptr = 6576567667788
        l uintptr = 5
    )
  
    // Calling LoadUintptr method
    // with its parameters
    load_1 := atomic.LoadUintptr(&i)
    load_2 := atomic.LoadUintptr(&j)
    load_3 := atomic.LoadUintptr(&k)
    load_4 := atomic.LoadUintptr(&l)
  
    // Displays uintptr value
    // loaded in the *addr
    fmt.Println(load_1)
    fmt.Println(load_2)
    fmt.Println(load_3)
    fmt.Println(load_4)
}

chevron_right


Output:

98
255
6576567667788
5

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// LoadUintptr function in Golang
  
// Including main package
package main
  
// Importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Declaring u
    var u uintptr
  
    // For loop
    for i := 1; i < 1000; i += 1 {
  
        // Function with
        // AddUintptr method
        go func() {
            atomic.AddUintptr(&u, 9)
        }()
    }
  
    // Prints loaded values address
    fmt.Println(atomic.LoadUintptr(&u))
}

chevron_right


Output:

1818   // A random value is returned in each run

In the above example, the new values are returned from AddUintptr() method in each call until the loop stops, LoadUintptr() method loads these new uintptr values. And these values are stored in different addresses which can be random one so, the output of the LoadUintptr() method here in each run is different. So, here a random value is returned in the output.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.