Created
November 26, 2019 18:49
-
-
Save lxfontes/c15745f4ad9d605ab55be2a48952094e to your computer and use it in GitHub Desktop.
go atomic swappointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync/atomic" | |
"unsafe" | |
) | |
type thing struct { | |
name string | |
} | |
type tracker struct { | |
pointer *thing | |
} | |
func main() { | |
xx := &thing{name: "ORIGINAL THING"} | |
yy := &thing{name: "ANOTHER THING"} | |
fmt.Printf("address for original %d, address for new %d\n", &xx, &yy) | |
t := &tracker{pointer: xx} | |
fmt.Printf("before: mem address %d, value %s\n", &t.pointer, t.pointer.name) | |
atomic.SwapPointer((*unsafe.Pointer)((unsafe.Pointer)(&t.pointer)), unsafe.Pointer(yy)) | |
fmt.Printf("after: mem address %d, value %s\n", &t.pointer, t.pointer.name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment