atomic.SwapPointer() Function in Golang With Examples

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The SwapPointer() function in Go language is used to atomically store new value into *addr and returns the previous *addr 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 SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)

Here, addr indicates address. And new is the new unsafe.Pointer value and old is the older unsafe.Pointer value.

Note: (*unsafe.Pointer) is the pointer to a unsafe.Pointer value. And unsafe.Pointer type is helpful in enabling transitions between arbitrary types and builtin uintptr type. Moreover, unsafe is a package that is helpful in type safety of Go programs.

Return Value: It stores the new unsafe.Pointer value into the *addr and returns the previous *addr value.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// SwapPointer function in Golang
  
// Including main package
package main
  
// Importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
    "unsafe"
)
  
// Defining a struct type L
type L struct{ x, y, z int }
  
// Declaring pointer to L struct type
var PL *L
  
// Main function
func main() {
  
    // Defining *addr unsafe.Pointer
    var unsafepL = (*unsafe.Pointer)(unsafe.Pointer(&PL))
  
    // Defining values 
    // of unsafe.Pointer
    var px, py L
  
    // Storing value to the pointer
    atomic.StorePointer(
        unsafepL, unsafe.Pointer(&px))
  
    // Calling SwapPointer() method
    px1 := atomic.SwapPointer(unsafepL,
                  unsafe.Pointer(&py))
  
    // Returns true if swapped
    fmt.Println((*L)(px1) == &px)
  
    // Prints output
    fmt.Println(px1)
}

chevron_right


Output:

true
0xc0000c2000  // Can be different at different run times

Here, the StorePointer method adds value to the *addr, then SwapPointer method atomically store new value into *addr and returns the old value. And, here swapping is accomplished hence, true is returned and the value of unsafe.Pointer returned here can be different at different run times.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the usage of
// SwapPointer function in Golang
  
// Including main package
package main
  
// Importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
    "unsafe"
)
  
// Defining a struct type L
type L struct{ x, y, z int }
  
// Declaring pointer
// to L struct type
var PL *L
  
// Main function
func main() {
  
    // Defining *addr unsafe.Pointer
    var unsafepL = (*unsafe.Pointer)(unsafe.Pointer(&PL))
  
    // Defining values of unsafe.Pointer
    var px, py L
  
    // Calling SwapPointer() method
    px1 := atomic.SwapPointer(unsafepL,
                  unsafe.Pointer(&py))
  
    // Returns true if swapped
    fmt.Println((*L)(px1) == &px)
  
    // Prints output
    fmt.Println(&px1)
}

chevron_right


Output:

false
0xc00000e028  // Can be different at different run times

Here, false is returned as here the unsafe.pointer is not stored before so, SwapPointer() method was not able to swap the stated value. Moreover, the address value returned here is the address of px1 and the value of px1 will be nil as swapping is not performed.




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.