Standard library / sync/atomic

atomic.AddUintptr()

Atomically add delta and return the new value. Integer overflow follows the type width. Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '15\n15\n'.

Formal Syntax & Structure

go syntaxgo
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)

Parameters & Attributes

NameTypeDescription
addr*uintptrPointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently.
deltauintptrAmount to add; for WaitGroup, change in outstanding task count.

Return Value

(new uintptr)

New value after addition.

Working Example

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