Standard library / sync/atomic

atomic.StoreUintptr()

Atomically replace the current value. Other shared data needs its own synchronization protocol. Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '15\n'.

Formal Syntax & Structure

go syntaxgo
func StoreUintptr(addr *uintptr, val uintptr)

Parameters & Attributes

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

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic")
func main() {  x := new(uintptr)  atomic.StoreUintptr(x, 12)  atomic.StoreUintptr(x, 15)  fmt.Println(atomic.LoadUintptr(x))}
Go Reference