atomic.StoreInt64() Function in Golang With Examples

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The StoreInt64() 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 StoreInt64(addr *int64, val int64)

Here, addr indicates address.

Note: (*int64) is the pointer to a int64 value. However, int64 contains the set of all signed 64-bit integers from -9223372036854775808 to 9223372036854775807.

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
// StoreInt64 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 int64
        y int64
    )
  
    // Using StoreInt64 method 
    // with its parameters
    atomic.StoreInt64(&x, 6777676777)
    atomic.StoreInt64(&y, 98877)
  
    // Displays the value stored in addr
    fmt.Println(atomic.LoadInt64(&x))
    fmt.Println(atomic.LoadInt64(&y))
}

chevron_right


Output:

6777676777
98877

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// StoreInt64 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 int64
    )
  
    // Using StoreInt64 method
    // with its parameters
    atomic.StoreInt64(&x, 3654567899788)
  
    // Loading the stored val
    z := atomic.LoadInt64(&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

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.