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:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var (
i uintptr = 98
j uintptr = 255
k uintptr = 6576567667788
l uintptr = 5
)
load_1 := atomic.LoadUintptr(&i)
load_2 := atomic.LoadUintptr(&j)
load_3 := atomic.LoadUintptr(&k)
load_4 := atomic.LoadUintptr(&l)
fmt.Println(load_1)
fmt.Println(load_2)
fmt.Println(load_3)
fmt.Println(load_4)
}
|
Output:
98
255
6576567667788
5
Example 2:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var u uintptr
for i := 1; i < 1000; i += 1 {
go func() {
atomic.AddUintptr(&u, 9)
}()
}
fmt.Println(atomic.LoadUintptr(&u))
}
|
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.
Last Updated :
01 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...