atomic.LoadUint64() 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 LoadUint64() 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 LoadUint64(addr *uint64) (val uint64)
Here, addr indicates address.
Note: (*uint64) is the pointer to a uint64 value. However, uint64 contains the set of all unsigned 64-bit integers from 0 to 18446744073709551615.
Return value: It returns the value loaded to the *addr.
Example 1:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var (
i uint64 = 587786787
j uint64 = 9
k uint64 = 78678844556666
l uint64 = 3446
)
load_1 := atomic.LoadUint64(&i)
load_2 := atomic.LoadUint64(&j)
load_3 := atomic.LoadUint64(&k)
load_4 := atomic.LoadUint64(&l)
fmt.Println(load_1)
fmt.Println(load_2)
fmt.Println(load_3)
fmt.Println(load_4)
}
|
Output:
587786787
9
78678844556666
3446
Example 2:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var u uint64
for i := 4; i < 200; i += 1 {
go func() {
atomic.AddUint64(&u, 6)
}()
}
fmt.Println(atomic.LoadUint64(&u))
}
|
Output:
1068 // A random value is returned in each run
In the above example, the new values are returned from AddUint64() method in each call until the loop stops, LoadUint64() method loads these new uint64 values. And these values are stored in different addresses which can be random one so, the output of the LoadUint32() method here in each run is different. So, here a random value is returned in the output.
Share your thoughts in the comments
Please Login to comment...