atomic.CompareAndSwapInt64() Function in Golang With Examples

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The CompareAndSwapInt64() function in Go language is used to perform the compare and swap operation for an int64 value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.

Syntax:

func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)

Here, addr indicates address, old indicates int64 value that is the old swapped value which is returned from the swapped operation, and new is the int64 new value that will swap itself from the old swapped value.

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

Return Value: It returns true if swapping is accomplished else it returns false.



Below examples illustrates the use of the above method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to illustrate the usage of
// CompareAndSwapInt64 function
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Assigning variable values to the int64
    var (
        i int64 = 686788787
    )
  
    // Swapping
    var old_value = atomic.SwapInt64(&i, 56677)
  
    // Printing old value and swapped value
    fmt.Println("Swapped:", i, ", old value:", old_value)
  
    // Calling CompareAndSwapInt64 
    // method with its parameters
    Swap := atomic.CompareAndSwapInt64(&i, 56677, 908998)
  
    // Displays true if swapped else false
    fmt.Println(Swap)
    fmt.Println("The Value of i is: ",i)
}

chevron_right


Output:

Swapped: 56677 , old value: 686788787
true
The Value of i is:  908998

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to illustrate the usage of
// CompareAndSwapInt64 function
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Assigning variable values to the int64
    var (
        i int64 = 686788787
    )
  
    // Swapping
    var old_value = atomic.SwapInt64(&i, 56677)
  
    // Printing old value and swapped value
    fmt.Println("Swapped:", i, ", old value:", old_value)
  
    // Calling CompareAndSwapInt64 
    // method with its parameters
    Swap := atomic.CompareAndSwapInt64(&i, 686788787, 908998)
  
    // Displays true if swapped else false
    fmt.Println(Swap)
    fmt.Println(i)
}

chevron_right


Output:

Swapped: 56677, old value: 686788787
false
56677

Here, the old value in the CompareAndSwapInt64 method must be the swapped value returned from the SwapInt64 method. And here the swapping is not performed so false is returned.




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.