Standard library / sync/atomic

atomic.StorePointer()

Atomically publish an unsafe.Pointer. This does not synchronize arbitrary future mutations of its pointee. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)

Parameters & Attributes

NameTypeDescription
addr*unsafe.PointerPointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently.
valunsafe.PointerValue to store; context values should be request-scoped.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic"  "unsafe")
func main() {  a, b := 7, 9  _ = b  var x unsafe.Pointer  atomic.StorePointer(&x, unsafe.Pointer(&a))  fmt.Println(*(*int)(atomic.LoadPointer(&x)))}
Go Reference