Movatterモバイル変換


[0]ホーム

URL:


atomic

packagestandard library
go1.24.5Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2025 License:BSD-3-ClauseImports:1Imported by:213,489

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package atomic provides low-level atomic memory primitivesuseful for implementing synchronization algorithms.

These functions require great care to be used correctly.Except for special, low-level applications, synchronization is betterdone with channels or the facilities of thesync package.Share memory by communicating;don't communicate by sharing memory.

The swap operation, implemented by the SwapT functions, is the atomicequivalent of:

old = *addr*addr = newreturn old

The compare-and-swap operation, implemented by the CompareAndSwapTfunctions, is the atomic equivalent of:

if *addr == old {*addr = newreturn true}return false

The add operation, implemented by the AddT functions, is the atomicequivalent of:

*addr += deltareturn *addr

The load and store operations, implemented by the LoadT and StoreTfunctions, are the atomic equivalents of "return *addr" and"*addr = val".

In the terminology ofthe Go memory model, if the effect ofan atomic operation A is observed by atomic operation B,then A “synchronizes before” B.Additionally, all the atomic operations executed in a programbehave as though executed in some sequentially consistent order.This definition provides the same semantics asC++'s sequentially consistent atomics and Java's volatile variables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcAddInt32

func AddInt32(addr *int32, deltaint32) (newint32)

AddInt32 atomically adds delta to *addr and returns the new value.Consider using the more ergonomic and less error-proneInt32.Add instead.

funcAddInt64

func AddInt64(addr *int64, deltaint64) (newint64)

AddInt64 atomically adds delta to *addr and returns the new value.Consider using the more ergonomic and less error-proneInt64.Add instead(particularly if you target 32-bit platforms; see the bugs section).

funcAddUint32

func AddUint32(addr *uint32, deltauint32) (newuint32)

AddUint32 atomically adds delta to *addr and returns the new value.To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)).In particular, to decrement x, do AddUint32(&x, ^uint32(0)).Consider using the more ergonomic and less error-proneUint32.Add instead.

funcAddUint64

func AddUint64(addr *uint64, deltauint64) (newuint64)

AddUint64 atomically adds delta to *addr and returns the new value.To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)).In particular, to decrement x, do AddUint64(&x, ^uint64(0)).Consider using the more ergonomic and less error-proneUint64.Add instead(particularly if you target 32-bit platforms; see the bugs section).

funcAddUintptr

func AddUintptr(addr *uintptr, deltauintptr) (newuintptr)

AddUintptr atomically adds delta to *addr and returns the new value.Consider using the more ergonomic and less error-proneUintptr.Add instead.

funcAndInt32added ingo1.23.0

func AndInt32(addr *int32, maskint32) (oldint32)

AndInt32 atomically performs a bitwise AND operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneInt32.And instead.

funcAndInt64added ingo1.23.0

func AndInt64(addr *int64, maskint64) (oldint64)

AndInt64 atomically performs a bitwise AND operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneInt64.And instead.

funcAndUint32added ingo1.23.0

func AndUint32(addr *uint32, maskuint32) (olduint32)

AndUint32 atomically performs a bitwise AND operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneUint32.And instead.

funcAndUint64added ingo1.23.0

func AndUint64(addr *uint64, maskuint64) (olduint64)

AndUint64 atomically performs a bitwise AND operation on *addr using the bitmask provided as maskand returns the old.Consider using the more ergonomic and less error-proneUint64.And instead.

funcAndUintptradded ingo1.23.0

func AndUintptr(addr *uintptr, maskuintptr) (olduintptr)

AndUintptr atomically performs a bitwise AND operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneUintptr.And instead.

funcCompareAndSwapInt32

func CompareAndSwapInt32(addr *int32, old, newint32) (swappedbool)

CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.Consider using the more ergonomic and less error-proneInt32.CompareAndSwap instead.

funcCompareAndSwapInt64

func CompareAndSwapInt64(addr *int64, old, newint64) (swappedbool)

CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.Consider using the more ergonomic and less error-proneInt64.CompareAndSwap instead(particularly if you target 32-bit platforms; see the bugs section).

funcCompareAndSwapPointer

func CompareAndSwapPointer(addr *unsafe.Pointer, old, newunsafe.Pointer) (swappedbool)

CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.Consider using the more ergonomic and less error-pronePointer.CompareAndSwap instead.

funcCompareAndSwapUint32

func CompareAndSwapUint32(addr *uint32, old, newuint32) (swappedbool)

CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.Consider using the more ergonomic and less error-proneUint32.CompareAndSwap instead.

funcCompareAndSwapUint64

func CompareAndSwapUint64(addr *uint64, old, newuint64) (swappedbool)

CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.Consider using the more ergonomic and less error-proneUint64.CompareAndSwap instead(particularly if you target 32-bit platforms; see the bugs section).

funcCompareAndSwapUintptr

func CompareAndSwapUintptr(addr *uintptr, old, newuintptr) (swappedbool)

CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.Consider using the more ergonomic and less error-proneUintptr.CompareAndSwap instead.

funcLoadInt32

func LoadInt32(addr *int32) (valint32)

LoadInt32 atomically loads *addr.Consider using the more ergonomic and less error-proneInt32.Load instead.

funcLoadInt64

func LoadInt64(addr *int64) (valint64)

LoadInt64 atomically loads *addr.Consider using the more ergonomic and less error-proneInt64.Load instead(particularly if you target 32-bit platforms; see the bugs section).

funcLoadPointer

func LoadPointer(addr *unsafe.Pointer) (valunsafe.Pointer)

LoadPointer atomically loads *addr.Consider using the more ergonomic and less error-pronePointer.Load instead.

funcLoadUint32

func LoadUint32(addr *uint32) (valuint32)

LoadUint32 atomically loads *addr.Consider using the more ergonomic and less error-proneUint32.Load instead.

funcLoadUint64

func LoadUint64(addr *uint64) (valuint64)

LoadUint64 atomically loads *addr.Consider using the more ergonomic and less error-proneUint64.Load instead(particularly if you target 32-bit platforms; see the bugs section).

funcLoadUintptr

func LoadUintptr(addr *uintptr) (valuintptr)

LoadUintptr atomically loads *addr.Consider using the more ergonomic and less error-proneUintptr.Load instead.

funcOrInt32added ingo1.23.0

func OrInt32(addr *int32, maskint32) (oldint32)

OrInt32 atomically performs a bitwise OR operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneInt32.Or instead.

funcOrInt64added ingo1.23.0

func OrInt64(addr *int64, maskint64) (oldint64)

OrInt64 atomically performs a bitwise OR operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneInt64.Or instead.

funcOrUint32added ingo1.23.0

func OrUint32(addr *uint32, maskuint32) (olduint32)

OrUint32 atomically performs a bitwise OR operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneUint32.Or instead.

funcOrUint64added ingo1.23.0

func OrUint64(addr *uint64, maskuint64) (olduint64)

OrUint64 atomically performs a bitwise OR operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneUint64.Or instead.

funcOrUintptradded ingo1.23.0

func OrUintptr(addr *uintptr, maskuintptr) (olduintptr)

OrUintptr atomically performs a bitwise OR operation on *addr using the bitmask provided as maskand returns the old value.Consider using the more ergonomic and less error-proneUintptr.Or instead.

funcStoreInt32

func StoreInt32(addr *int32, valint32)

StoreInt32 atomically stores val into *addr.Consider using the more ergonomic and less error-proneInt32.Store instead.

funcStoreInt64

func StoreInt64(addr *int64, valint64)

StoreInt64 atomically stores val into *addr.Consider using the more ergonomic and less error-proneInt64.Store instead(particularly if you target 32-bit platforms; see the bugs section).

funcStorePointer

func StorePointer(addr *unsafe.Pointer, valunsafe.Pointer)

StorePointer atomically stores val into *addr.Consider using the more ergonomic and less error-pronePointer.Store instead.

funcStoreUint32

func StoreUint32(addr *uint32, valuint32)

StoreUint32 atomically stores val into *addr.Consider using the more ergonomic and less error-proneUint32.Store instead.

funcStoreUint64

func StoreUint64(addr *uint64, valuint64)

StoreUint64 atomically stores val into *addr.Consider using the more ergonomic and less error-proneUint64.Store instead(particularly if you target 32-bit platforms; see the bugs section).

funcStoreUintptr

func StoreUintptr(addr *uintptr, valuintptr)

StoreUintptr atomically stores val into *addr.Consider using the more ergonomic and less error-proneUintptr.Store instead.

funcSwapInt32added ingo1.2

func SwapInt32(addr *int32, newint32) (oldint32)

SwapInt32 atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-proneInt32.Swap instead.

funcSwapInt64added ingo1.2

func SwapInt64(addr *int64, newint64) (oldint64)

SwapInt64 atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-proneInt64.Swap instead(particularly if you target 32-bit platforms; see the bugs section).

funcSwapPointeradded ingo1.2

func SwapPointer(addr *unsafe.Pointer, newunsafe.Pointer) (oldunsafe.Pointer)

SwapPointer atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-pronePointer.Swap instead.

funcSwapUint32added ingo1.2

func SwapUint32(addr *uint32, newuint32) (olduint32)

SwapUint32 atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-proneUint32.Swap instead.

funcSwapUint64added ingo1.2

func SwapUint64(addr *uint64, newuint64) (olduint64)

SwapUint64 atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-proneUint64.Swap instead(particularly if you target 32-bit platforms; see the bugs section).

funcSwapUintptradded ingo1.2

func SwapUintptr(addr *uintptr, newuintptr) (olduintptr)

SwapUintptr atomically stores new into *addr and returns the previous *addr value.Consider using the more ergonomic and less error-proneUintptr.Swap instead.

Types

typeBooladded ingo1.19

type Bool struct {// contains filtered or unexported fields}

A Bool is an atomic boolean value.The zero value is false.

func (*Bool)CompareAndSwapadded ingo1.19

func (x *Bool) CompareAndSwap(old, newbool) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for the boolean value x.

func (*Bool)Loadadded ingo1.19

func (x *Bool) Load()bool

Load atomically loads and returns the value stored in x.

func (*Bool)Storeadded ingo1.19

func (x *Bool) Store(valbool)

Store atomically stores val into x.

func (*Bool)Swapadded ingo1.19

func (x *Bool) Swap(newbool) (oldbool)

Swap atomically stores new into x and returns the previous value.

typeInt32added ingo1.19

type Int32 struct {// contains filtered or unexported fields}

An Int32 is an atomic int32. The zero value is zero.

func (*Int32)Addadded ingo1.19

func (x *Int32) Add(deltaint32) (newint32)

Add atomically adds delta to x and returns the new value.

func (*Int32)Andadded ingo1.23.0

func (x *Int32) And(maskint32) (oldint32)

And atomically performs a bitwise AND operation on x using the bitmaskprovided as mask and returns the old value.

func (*Int32)CompareAndSwapadded ingo1.19

func (x *Int32) CompareAndSwap(old, newint32) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Int32)Loadadded ingo1.19

func (x *Int32) Load()int32

Load atomically loads and returns the value stored in x.

func (*Int32)Oradded ingo1.23.0

func (x *Int32) Or(maskint32) (oldint32)

Or atomically performs a bitwise OR operation on x using the bitmaskprovided as mask and returns the old value.

func (*Int32)Storeadded ingo1.19

func (x *Int32) Store(valint32)

Store atomically stores val into x.

func (*Int32)Swapadded ingo1.19

func (x *Int32) Swap(newint32) (oldint32)

Swap atomically stores new into x and returns the previous value.

typeInt64added ingo1.19

type Int64 struct {// contains filtered or unexported fields}

An Int64 is an atomic int64. The zero value is zero.

func (*Int64)Addadded ingo1.19

func (x *Int64) Add(deltaint64) (newint64)

Add atomically adds delta to x and returns the new value.

func (*Int64)Andadded ingo1.23.0

func (x *Int64) And(maskint64) (oldint64)

And atomically performs a bitwise AND operation on x using the bitmaskprovided as mask and returns the old value.

func (*Int64)CompareAndSwapadded ingo1.19

func (x *Int64) CompareAndSwap(old, newint64) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Int64)Loadadded ingo1.19

func (x *Int64) Load()int64

Load atomically loads and returns the value stored in x.

func (*Int64)Oradded ingo1.23.0

func (x *Int64) Or(maskint64) (oldint64)

Or atomically performs a bitwise OR operation on x using the bitmaskprovided as mask and returns the old value.

func (*Int64)Storeadded ingo1.19

func (x *Int64) Store(valint64)

Store atomically stores val into x.

func (*Int64)Swapadded ingo1.19

func (x *Int64) Swap(newint64) (oldint64)

Swap atomically stores new into x and returns the previous value.

typePointeradded ingo1.19

type Pointer[Tany] struct {// contains filtered or unexported fields}

A Pointer is an atomic pointer of type *T. The zero value is a nil *T.

func (*Pointer[T])CompareAndSwapadded ingo1.19

func (x *Pointer[T]) CompareAndSwap(old, new *T) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Pointer[T])Loadadded ingo1.19

func (x *Pointer[T]) Load() *T

Load atomically loads and returns the value stored in x.

func (*Pointer[T])Storeadded ingo1.19

func (x *Pointer[T]) Store(val *T)

Store atomically stores val into x.

func (*Pointer[T])Swapadded ingo1.19

func (x *Pointer[T]) Swap(new *T) (old *T)

Swap atomically stores new into x and returns the previous value.

typeUint32added ingo1.19

type Uint32 struct {// contains filtered or unexported fields}

A Uint32 is an atomic uint32. The zero value is zero.

func (*Uint32)Addadded ingo1.19

func (x *Uint32) Add(deltauint32) (newuint32)

Add atomically adds delta to x and returns the new value.

func (*Uint32)Andadded ingo1.23.0

func (x *Uint32) And(maskuint32) (olduint32)

And atomically performs a bitwise AND operation on x using the bitmaskprovided as mask and returns the old value.

func (*Uint32)CompareAndSwapadded ingo1.19

func (x *Uint32) CompareAndSwap(old, newuint32) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Uint32)Loadadded ingo1.19

func (x *Uint32) Load()uint32

Load atomically loads and returns the value stored in x.

func (*Uint32)Oradded ingo1.23.0

func (x *Uint32) Or(maskuint32) (olduint32)

Or atomically performs a bitwise OR operation on x using the bitmaskprovided as mask and returns the old value.

func (*Uint32)Storeadded ingo1.19

func (x *Uint32) Store(valuint32)

Store atomically stores val into x.

func (*Uint32)Swapadded ingo1.19

func (x *Uint32) Swap(newuint32) (olduint32)

Swap atomically stores new into x and returns the previous value.

typeUint64added ingo1.19

type Uint64 struct {// contains filtered or unexported fields}

A Uint64 is an atomic uint64. The zero value is zero.

func (*Uint64)Addadded ingo1.19

func (x *Uint64) Add(deltauint64) (newuint64)

Add atomically adds delta to x and returns the new value.

func (*Uint64)Andadded ingo1.23.0

func (x *Uint64) And(maskuint64) (olduint64)

And atomically performs a bitwise AND operation on x using the bitmaskprovided as mask and returns the old value.

func (*Uint64)CompareAndSwapadded ingo1.19

func (x *Uint64) CompareAndSwap(old, newuint64) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Uint64)Loadadded ingo1.19

func (x *Uint64) Load()uint64

Load atomically loads and returns the value stored in x.

func (*Uint64)Oradded ingo1.23.0

func (x *Uint64) Or(maskuint64) (olduint64)

Or atomically performs a bitwise OR operation on x using the bitmaskprovided as mask and returns the old value.

func (*Uint64)Storeadded ingo1.19

func (x *Uint64) Store(valuint64)

Store atomically stores val into x.

func (*Uint64)Swapadded ingo1.19

func (x *Uint64) Swap(newuint64) (olduint64)

Swap atomically stores new into x and returns the previous value.

typeUintptradded ingo1.19

type Uintptr struct {// contains filtered or unexported fields}

A Uintptr is an atomic uintptr. The zero value is zero.

func (*Uintptr)Addadded ingo1.19

func (x *Uintptr) Add(deltauintptr) (newuintptr)

Add atomically adds delta to x and returns the new value.

func (*Uintptr)Andadded ingo1.23.0

func (x *Uintptr) And(maskuintptr) (olduintptr)

And atomically performs a bitwise AND operation on x using the bitmaskprovided as mask and returns the old value.

func (*Uintptr)CompareAndSwapadded ingo1.19

func (x *Uintptr) CompareAndSwap(old, newuintptr) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Uintptr)Loadadded ingo1.19

func (x *Uintptr) Load()uintptr

Load atomically loads and returns the value stored in x.

func (*Uintptr)Oradded ingo1.23.0

func (x *Uintptr) Or(maskuintptr) (olduintptr)

Or atomically performs a bitwise OR operation on x using the bitmaskprovided as mask and returns the updated value after the OR operation.

func (*Uintptr)Storeadded ingo1.19

func (x *Uintptr) Store(valuintptr)

Store atomically stores val into x.

func (*Uintptr)Swapadded ingo1.19

func (x *Uintptr) Swap(newuintptr) (olduintptr)

Swap atomically stores new into x and returns the previous value.

typeValueadded ingo1.4

type Value struct {// contains filtered or unexported fields}

A Value provides an atomic load and store of a consistently typed value.The zero value for a Value returns nil fromValue.Load.OnceValue.Store has been called, a Value must not be copied.

A Value must not be copied after first use.

Example (Config)

The following example shows how to use Value for periodic program config updatesand propagation of the changes to worker goroutines.

package mainimport ("sync/atomic""time")func loadConfig() map[string]string {return make(map[string]string)}func requests() chan int {return make(chan int)}func main() {var config atomic.Value // holds current server configuration// Create initial config value and store into config.config.Store(loadConfig())go func() {// Reload config every 10 seconds// and update config value with the new version.for {time.Sleep(10 * time.Second)config.Store(loadConfig())}}()// Create worker goroutines that handle incoming requests// using the latest config value.for i := 0; i < 10; i++ {go func() {for r := range requests() {c := config.Load()// Handle request r using config c._, _ = r, c}}()}}

Example (ReadMostly)

The following example shows how to maintain a scalable frequently read,but infrequently updated data structure using copy-on-write idiom.

package mainimport ("sync""sync/atomic")func main() {type Map map[string]stringvar m atomic.Valuem.Store(make(Map))var mu sync.Mutex // used only by writers// read function can be used to read the data without further synchronizationread := func(key string) (val string) {m1 := m.Load().(Map)return m1[key]}// insert function can be used to update the data without further synchronizationinsert := func(key, val string) {mu.Lock() // synchronize with other potential writersdefer mu.Unlock()m1 := m.Load().(Map) // load current value of the data structurem2 := make(Map)      // create a new valuefor k, v := range m1 {m2[k] = v // copy all data from the current object to the new one}m2[key] = val // do the update that we needm.Store(m2)   // atomically replace the current object with the new one// At this point all new readers start working with the new version.// The old version will be garbage collected once the existing readers// (if any) are done with it.}_, _ = read, insert}

func (*Value)CompareAndSwapadded ingo1.17

func (v *Value) CompareAndSwap(old, newany) (swappedbool)

CompareAndSwap executes the compare-and-swap operation for theValue.

All calls to CompareAndSwap for a given Value must use values of the sameconcrete type. CompareAndSwap of an inconsistent type panics, as doesCompareAndSwap(old, nil).

func (*Value)Loadadded ingo1.4

func (v *Value) Load() (valany)

Load returns the value set by the most recent Store.It returns nil if there has been no call to Store for this Value.

func (*Value)Storeadded ingo1.4

func (v *Value) Store(valany)

Store sets the value of theValue v to val.All calls to Store for a given Value must use values of the same concrete type.Store of an inconsistent type panics, as does Store(nil).

func (*Value)Swapadded ingo1.17

func (v *Value) Swap(newany) (oldany)

Swap stores new into Value and returns the previous value. It returns nil ifthe Value is empty.

All calls to Swap for a given Value must use values of the same concretetype. Swap of an inconsistent type panics, as does Swap(nil).

Notes

Bugs

  • On 386, the 64-bit functions use instructions unavailable before the Pentium MMX.

    On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core.

    On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to arrangefor 64-bit alignment of 64-bit words accessed atomically via the primitiveatomic functions (typesInt64 andUint64 are automatically aligned).The first word in an allocated struct, array, or slice; in a globalvariable; or in a local variable (because on 32-bit architectures, thesubject of 64-bit atomic operations will escape to the heap) can berelied upon to be 64-bit aligned.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp