atomic

原文:https://pkg.go.dev/sync/atomic@go1.23.0

image-20231110170148590

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

atomic包提供了低级别的原子内存原语,用于实现同步算法。

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

​ 这些函数需要非常小心才能正确使用。除特殊的低级应用外,最好使用通道或sync包的工具进行同步。通过通信来共享内存,而不是通过共享内存来通信。

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

​ 由SwapT函数实现的交换操作,其原子操作相当于:

1
2
3
old = *addr
*addr = new
return old

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

​ 由CompareAndSwapT函数实现的比较并交换操作,其原子操作相当于:

1
2
3
4
5
if *addr == old {
	*addr = new
	return true
}
return false

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

​ 由AddT函数实现的加法操作,其原子操作相当于:

1
2
*addr += delta
return *addr

The load and store operations, implemented by the LoadT and StoreT functions, are the atomic equivalents of “return *addr” and “*addr = val”.

​ 由LoadTStoreT函数实现的加载和存储操作,其原子操作相当于“return *addr”和“*addr = val”。

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

​ 在Go内存模型的术语中,如果一个原子操作A的效果被原子操作B观察到,那么A“在B之前同步”。此外,程序中执行的所有原子操作表现为好像是以某种顺序一致的方式执行的。这个定义提供了与C++的顺序一致原子操作和Java的易失性变量相同的语义。

常量

This section is empty.

变量

This section is empty.

函数

func AddInt32

1
func AddInt32(addr *int32, delta int32) (new int32)

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

AddInt32函数原子地将delta添加到*addr,并返回new值。考虑使用更符合人体工程学和更不容易出错的Int32.Add代替。

func AddInt64

1
func AddInt64(addr *int64, delta int64) (new int64)

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

AddInt64函数原子地将delta添加到*addr,并返回new值。如果您的目标是32位平台,请考虑使用更符合人体工程学和更不容易出错的Int64.Add代替(请参见错误部分)。

func AddUint32

1
func AddUint32(addr *uint32, delta uint32) (new uint32)

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-prone Uint32.Add instead.

AddUint32函数原子地将delta添加到*addr,并返回new 值。要从x减去已知正常数值c,请执行AddUint32(&x,^uint32(c-1))。特别地,要将x减少1,请执行AddUint32(&x,^uint32(0))。考虑使用更符合人体工程学和更不容易出错的Uint32.Add代替。

func AddUint64

1
func AddUint64(addr *uint64, delta uint64) (new uint64)

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-prone Uint64.Add instead (particularly if you target 32-bit platforms; see the bugs section).

AddUint64函数原子地将delta添加到*addr,并返回new值。要从x减去已知正常数值c,请执行AddUint64(&x,^uint64(c-1))。特别地,要将x减少1,请执行AddUint64(&x,^uint64(0))。如果您的目标是32位平台,请考虑使用更符合人体工程学和更不容易出错的Uint64.Add代替(请参见错误部分)。

func AddUintptr

1
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)

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

AddUintptr函数原子地将delta添加到*addr,并返回new值。考虑使用更符合人体工程学和更不容易出错的Uintptr.Add代替。

func AndInt32 <- go1.23.0

1
func AndInt32(addr *int32, mask int32) (old int32)

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

AndInt32 使用提供的位掩码 mask*addr 执行原子按位与操作,并返回旧值。建议使用更方便且不易出错的 Int32.And 代替。

func AndInt64 <- go1.23.0

1
func AndInt64(addr *int64, mask int64) (old int64)

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

AndInt64 使用提供的位掩码 mask*addr 执行原子按位与操作,并返回旧值。建议使用更方便且不易出错的 Int64.And 代替。

func AndUint32 <- go1.23.0

1
func AndUint32(addr *uint32, mask uint32) (old uint32)

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

AndUint32 使用提供的位掩码 mask*addr 执行原子按位与操作,并返回旧值。建议使用更方便且不易出错的 Uint32.And 代替。

func AndUint64 <- go1.23.0

1
func AndUint64(addr *uint64, mask uint64) (old uint64)

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

AndUint64 使用提供的位掩码 mask*addr 执行原子按位与操作,并返回旧值。建议使用更方便且不易出错的 Uint64.And 代替。

func AndUintptr <- go1.23.0

1
func AndUintptr(addr *uintptr, mask uintptr) (old uintptr)

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

AndUintptr 使用提供的位掩码 mask*addr 执行原子按位与操作,并返回旧值。建议使用更方便且不易出错的 Uintptr.And 代替。

func CompareAndSwapInt32

1
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)

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

CompareAndSwapInt32函数执行 int32 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Int32.CompareAndSwap

func CompareAndSwapInt64

1
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)

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

CompareAndSwapInt64函数执行 int64 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Int64.CompareAndSwap (特别是针对 32 位平台;参见错误部分)。

func CompareAndSwapPointer

1
func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)

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

CompareAndSwapPointer函数执行 unsafe.Pointer 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Pointer.CompareAndSwap

func CompareAndSwapUint32

1
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)

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

CompareAndSwapUint32函数执行 uint32 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Uint32.CompareAndSwap

func CompareAndSwapUint64

1
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)

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

CompareAndSwapUint64函数执行 uint64 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Uint64.CompareAndSwap (特别是针对 32 位平台;参见错误部分)。

func CompareAndSwapUintptr

1
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)

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

CompareAndSwapUintptr函数执行 uintptr 值的比较并交换操作。建议使用更符合人体工程学和更少容易出错的 Uintptr.CompareAndSwap

func LoadInt32

1
func LoadInt32(addr *int32) (val int32)

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

LoadInt32函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Int32.Load

func LoadInt64

1
func LoadInt64(addr *int64) (val int64)

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

LoadInt64函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Int64.Load(特别是如果您的目标是32位平台;请参见错误部分)。

func LoadPointer

1
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)

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

LoadPointer函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Pointer.Load

func LoadUint32

1
func LoadUint32(addr *uint32) (val uint32)

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

LoadUint32函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Uint32.Load

func LoadUint64

1
func LoadUint64(addr *uint64) (val uint64)

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

LoadUint64函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Uint64.Load(特别是如果您的目标是32位平台;请参见错误部分)。

func LoadUintptr

1
func LoadUintptr(addr *uintptr) (val uintptr)

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

LoadUintptr函数原子地加载 *addr。考虑使用更符合人体工程学且不易出错的 Uintptr.Load

func OrInt32 <- go1.23.0

1
func OrInt32(addr *int32, mask int32) (old int32)

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

OrInt32 使用提供的位掩码 mask*addr 执行原子按位或操作,并返回旧值。建议使用更方便且不易出错的 Int32.Or 代替。

func OrInt64 <- go1.23.0

1
func OrInt64(addr *int64, mask int64) (old int64)

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

OrInt64 使用提供的位掩码 mask*addr 执行原子按位或操作,并返回旧值。建议使用更方便且不易出错的 Int64.Or 代替。

func OrUint32 <- go1.23.0

1
func OrUint32(addr *uint32, mask uint32) (old uint32)

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

OrUint32 使用提供的位掩码 mask*addr 执行原子按位或操作,并返回旧值。建议使用更方便且不易出错的 Uint32.Or 代替。

func OrUint64 <- go1.23.0

1
func OrUint64(addr *uint64, mask uint64) (old uint64)

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

OrUint64 使用提供的位掩码 mask*addr 执行原子按位或操作,并返回旧值。建议使用更方便且不易出错的 Uint64.Or 代替。

func OrUintptr <- go1.23.0

1
func OrUintptr(addr *uintptr, mask uintptr) (old uintptr)

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

OrUintptr 使用提供的位掩码 mask*addr 执行原子按位或操作,并返回旧值。建议使用更方便且不易出错的 Uintptr.Or 代替。

func StoreInt32

1
func StoreInt32(addr *int32, val int32)

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

StoreInt32函数原子地将 val 存储到 *addr。考虑使用更符合人体工程学且不易出错的 Int32.Store

func StoreInt64

1
func StoreInt64(addr *int64, val int64)

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

StoreInt64函数原子地将 val 存储到 *addr。考虑使用更符合人体工程学且不易出错的 Int64.Store(特别是如果您的目标是32位平台;请参见错误部分)。

func StorePointer

1
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)

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

StorePointer函数原子地将 val 存储到 *addr。考虑使用更符合人体工程学且不易出错的 Pointer.Store

func StoreUint32

1
func StoreUint32(addr *uint32, val uint32)

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

StoreUint32函数原子性地将val存储到*addr。考虑使用更符合人体工程学且更少出错的Uint32.Store

func StoreUint64

1
func StoreUint64(addr *uint64, val uint64)

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

StoreUint64函数原子性地将val存储到*addr。考虑使用更符合人体工程学且更少出错的Uint64.Store(特别是针对32位平台,参见bugs一节)。

func StoreUintptr

1
func StoreUintptr(addr *uintptr, val uintptr)

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

StoreUintptr函数原子性地将val存储到*addr。考虑使用更符合人体工程学且更少出错的Uintptr.Store

func SwapInt32 <- go1.2

1
func SwapInt32(addr *int32, new int32) (old int32)

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

SwapInt32函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Int32.Swap

func SwapInt64 <- go1.2

1
func SwapInt64(addr *int64, new int64) (old int64)

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

SwapInt64函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Int64.Swap(特别是针对32位平台,参见bugs一节)。

func SwapPointer <- go1.2

1
func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)

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

SwapPointer函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Pointer.Swap

func SwapUint32 <- go1.2

1
func SwapUint32(addr *uint32, new uint32) (old uint32)

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

SwapUint32函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Uint32.Swap

func SwapUint64 <- go1.2

1
func SwapUint64(addr *uint64, new uint64) (old uint64)

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

SwapUint64函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Uint64.Swap(特别是针对32位平台,参见bugs一节)。

func SwapUintptr <- go1.2

1
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)

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

SwapUintptr函数原子性地将new存储到*addr并返回先前的*addr值。考虑使用更符合人体工程学且更少出错的Uintptr.Swap

类型

type Bool <- go1.19

1
2
3
type Bool struct {
	// contains filtered or unexported fields
}

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

Bool结构体是一个原子布尔值。 零值为false

(*Bool) CompareAndSwap <- go1.19

1
func (x *Bool) CompareAndSwap(old, new bool) (swapped bool)

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

CompareAndSwap方法执行布尔值x的比较和交换操作。

(*Bool) Load <- go1.19

1
func (x *Bool) Load() bool

Load atomically loads and returns the value stored in x.

Load方法以原子方式加载并返回存储在x中的值。

(*Bool) Store <- go1.19

1
func (x *Bool) Store(val bool)

Store atomically stores val into x.

Store方法原子地将val存储到x中。

(*Bool) Swap <- go1.19

1
func (x *Bool) Swap(new bool) (old bool)

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

Swap方法以原子方式将new存储到x中并返回先前的值。

type Int32 <- go1.19

1
2
3
type Int32 struct {
	// contains filtered or unexported fields
}

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

Int32方法是一个原子int32。 零值为0

(*Int32) Add <- go1.19

1
func (x *Int32) Add(delta int32) (new int32)

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

Add方法原子地将delta添加到x并返回new值。

(*Int32) And <- go1.23.0

1
func (x *Int32) And(mask int32) (old int32)

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

And 使用提供的位掩码 maskx 执行原子按位与操作,并返回旧值。

(*Int32) CompareAndSwap <- go1.19

1
func (x *Int32) CompareAndSwap(old, new int32) (swapped bool)

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

CompareAndSwap方法执行x的比较和交换操作。

(*Int32) Load <- go1.19

1
func (x *Int32) Load() int32

Load atomically loads and returns the value stored in x.

Load方法原子地加载并返回存储在x中的值。

(*Int32) Or <- go1.23.0

1
func (x *Int32) Or(mask int32) (old int32)

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

Or 使用提供的位掩码 maskx 执行原子按位或操作,并返回旧值。

(*Int32) Store <- go1.19

1
func (x *Int32) Store(val int32)

Store atomically stores val into x.

Store方法原子地将val存储到x中。

(*Int32) Swap <- go1.19

1
func (x *Int32) Swap(new int32) (old int32)

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

Swap方法原子地将new存储到x中并返回先前的值。

type Int64 <- go1.19

1
2
3
type Int64 struct {
	// contains filtered or unexported fields
}

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

Int64方法是原子int64。零值为0

(*Int64) Add <- go1.19

1
func (x *Int64) Add(delta int64) (new int64)

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

Add方法原子地将delta添加到x并返回new值。

(*Int64) And <- go1.23.0

1
func (x *Int64) And(mask int64) (old int64)

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

And 使用提供的位掩码 maskx 执行原子按位与操作,并返回旧值。

(*Int64) CompareAndSwap <- go1.19

1
func (x *Int64) CompareAndSwap(old, new int64) (swapped bool)

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

CompareAndSwap方法执行x的比较并交换操作。

(*Int64) Load <- go1.19

1
func (x *Int64) Load() int64

Load atomically loads and returns the value stored in x.

Load方法原子地加载并返回存储在x中的值。

(*Int64) Or <- go1.23.0

1
func (x *Int64) Or(mask int64) (old int64)

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

Or 使用提供的位掩码 maskx 执行原子按位或操作,并返回旧值。

(*Int64) Store <- go1.19

1
func (x *Int64) Store(val int64)

Store atomically stores val into x.

Store方法原子地将val存储到x中。

(*Int64) Swap <- go1.19

1
func (x *Int64) Swap(new int64) (old int64)

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

Swap方法原子地将new存储到x中并返回先前的值。

type Pointer <- go1.19

1
2
3
type Pointer[T any] struct {
	// contains filtered or unexported fields
}

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

Pointer 是一个类型为 *T 的原子指针。零值是 nil 的 *T

(*Pointer[T]) CompareAndSwap <- go1.19

1
func (x *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool)

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

CompareAndSwap方法执行 x 的比较并交换操作。

(*Pointer[T]) Load <- go1.19

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

Load atomically loads and returns the value stored in x.

Load方法原子地加载并返回存储在 x 中的值。

(*Pointer[T]) Store <- go1.19

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

Store atomically stores val into x.

Store方法原子地将 val 存储到 x 中。

(*Pointer[T]) Swap <- go1.19

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

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

Swap方法原子地将 new 存储到 x 中,并返回先前的值。

type Uint32 <- go1.19

1
2
3
type Uint32 struct {
	// contains filtered or unexported fields
}

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

Uint32方法是一个原子 uint32。零值是0

(*Uint32) Add <- go1.19

1
func (x *Uint32) Add(delta uint32) (new uint32)

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

Add方法原子地将 delta 添加到 x 并返回new值。

(*Uint32) And <- go1.23.0

1
func (x *Uint32) And(mask uint32) (old uint32)

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

And 使用提供的位掩码 maskx 执行原子按位与操作,并返回旧值。

(*Uint32) CompareAndSwap <- go1.19

1
func (x *Uint32) CompareAndSwap(old, new uint32) (swapped bool)

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

CompareAndSwap方法执行 x 的比较并交换操作。

(*Uint32) Or <- go1.23.0

1
func (x *Uint32) Or(mask uint32) (old uint32)

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

Or 使用提供的位掩码 maskx 执行原子按位或操作,并返回旧值。

(*Uint32) Load <- go1.19

1
func (x *Uint32) Load() uint32

Load atomically loads and returns the value stored in x.

Load方法原子地加载并返回存储在x中的值。

(*Uint32) Store <- go1.19

1
func (x *Uint32) Store(val uint32)

Store atomically stores val into x.

Store方法原子地将val存储到x中。

(*Uint32) Swap <- go1.19

1
func (x *Uint32) Swap(new uint32) (old uint32)

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

Swap方法原子地将new存储到x中并返回先前的值。

type Uint64 <- go1.19

1
2
3
type Uint64 struct {
	// contains filtered or unexported fields
}

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

Uint64方法是一个原子性的uint64类型。零值为0

(*Uint64) Add <- go1.19

1
func (x *Uint64) Add(delta uint64) (new uint64)

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

Add方法原子性地将delta添加到x中并返回new值。

(*Uint64) And <- go1.23.0

1
func (x *Uint64) And(mask uint64) (old uint64)

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

And 使用提供的位掩码 maskx 执行原子按位与操作,并返回旧值。

(*Uint64) CompareAndSwap <- go1.19

1
func (x *Uint64) CompareAndSwap(old, new uint64) (swapped bool)

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

CompareAndSwap方法在x上执行比较并交换操作。

(*Uint64) Or <- go1.23.0

1
func (x *Uint64) Or(mask uint64) (old uint64)

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

Or 使用提供的位掩码 maskx 执行原子按位或操作,并返回旧值。

(*Uint64) Load <- go1.19

1
func (x *Uint64) Load() uint64

Load atomically loads and returns the value stored in x.

Load方法原子地加载并返回存储在x中的值。

(*Uint64) Store <- go1.19

1
func (x *Uint64) Store(val uint64)

Store atomically stores val into x.

Store方法原子地将val存储到x中。

(*Uint64) Swap <- go1.19

1
func (x *Uint64) Swap(new uint64) (old uint64)

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

Swap方法原子地将new存储到x中并返回先前的值。

type Uintptr <- go1.19

1
2
3
type Uintptr struct {
	// contains filtered or unexported fields
}

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

Uintptr是一个原子的uintptr类型。零值为0

(*Uintptr) Add <- go1.19

1
func (x *Uintptr) Add(delta uintptr) (new uintptr)

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

Add方法原子地将delta添加到x并返回new值。

(*Uintptr) And <- go1.23.0

1
func (x *Uintptr) And(mask uintptr) (old uintptr)

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

And 使用提供的位掩码 maskx 执行原子按位与操作,并返回旧值。

(*Uintptr) CompareAndSwap <- go1.19

1
func (x *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool)

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

CompareAndSwap方法执行x的比较和交换操作。

(*Uintptr) Load <- go1.19

1
func (x *Uintptr) Load() uintptr

Load atomically loads and returns the value stored in x.

Load方法以原子方式加载并返回存储在x中的值。

(*Uintptr) Or <- go1.23.0

1
func (x *Uintptr) Or(mask uintptr) (old uintptr)

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

Or 使用提供的位掩码 maskx 执行原子按位或操作,并返回按位或操作后的更新值。

(*Uintptr) Store <- go1.19

1
func (x *Uintptr) Store(val uintptr)

Store atomically stores val into x.

Store方法以原子方式将val存储到x中。

(*Uintptr) Swap <- go1.19

1
func (x *Uintptr) Swap(new uintptr) (old uintptr)

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

Swap方法以原子方式将new存储到x中并返回先前的值。

type Value <- go1.4

1
2
3
type Value struct {    
	v any
}

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

Value类型提供了一个一致类型值的原子加载和存储。Value类型的零值从Load返回nil。一旦调用了Store,就不能再复制Value

A Value must not be copied after first use.

​ 使用后不能再复制Value

Example (Config)

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

​ 以下示例展示了如何使用 Value 来进行定期程序配置更新,并将更改传播到工作 goroutines。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"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 中。
	config.Store(loadConfig())
	go func() {
		// Reload config every 10 seconds
		// and update config value with the new version.
        // 每隔 10 秒重新加载配置
		// 并使用新版本更新配置值。
		for {
			time.Sleep(10 * time.Second)
			config.Store(loadConfig())
		}
	}()
	// Create worker goroutines that handle incoming requests
	// using the latest config value.
    // 创建处理传入请求的工作 goroutine
	// 使用最新的配置值。
	for i := 0; i < 10; i++ {
		go func() {
			for r := range requests() {
				c := config.Load()
				// Handle request r using config c.
                // 使用配置 c 处理请求 r。
				_, _ = 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.

​ 以下示例展示了如何使用写时复制(copy-on-write)惯用语法来维护一个可扩展的频繁读取但不经常更新的数据结构。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
	"sync"
	"sync/atomic"
)

func main() {
	type Map map[string]string
	var m atomic.Value
	m.Store(make(Map))
	var mu sync.Mutex // used only by writers 仅由写入者使用
	// read function can be used to read the data without further synchronization
    // read 函数用于在无需进一步同步的情况下读取数据
	read := func(key string) (val string) {
		m1 := m.Load().(Map)
		return m1[key]
	}
	// insert function can be used to update the data without further synchronization
    // insert 函数用于在无需进一步同步的情况下更新数据
	insert := func(key, val string) {
		mu.Lock() // synchronize with other potential writers 与其他潜在写入者同步
		defer mu.Unlock()
		m1 := m.Load().(Map) // load current value of the data structure 加载当前数据结构的值
		m2 := make(Map)      // create a new value 创建新值
		for 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 need  执行我们需要的更新
		m.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
}

(*Value) CompareAndSwap <- go1.17

1
func (v *Value) CompareAndSwap(old, new any) (swapped bool)

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

CompareAndSwap方法为Value执行比较和交换操作。

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

​ 所有对给定ValueCompareAndSwap调用必须使用相同具体类型的值。类型不一致的CompareAndSwap会导致panic,CompareAndSwap(old, nil)也是如此。

(*Value) Load <- go1.4

1
func (v *Value) Load() (val any)

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

Load方法返回最近一次Store设置的值。如果没有为此Value调用Store,则返回nil

(*Value) Store <- go1.4

1
func (v *Value) Store(val any)

Store sets the value of the Value 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).

Store方法将Value v的值设置为val。所有对给定ValueStore调用必须使用相同具体类型的值。类型不一致的Store会导致panic,Store(nil)也是如此。

(*Value) Swap <- go1.17

1
func (v *Value) Swap(new any) (old any)

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

Swap方法操作将new值存储到Value中,并返回旧值。如果Value为空,则返回nil

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

​ 对于同一个 Value,所有 Swap方法的调用必须使用相同的具体类型的值。如果使用不一致的类型进行 Swap方法,会导致 panic,就像 Swap(nil) 一样。

Notes

Bugs

  • 在386上,64位函数使用Pentium MMX之前不可用的指令。

    在非Linux ARM上,64位函数使用ARMv6k核心之前不可用的指令。

    在ARM,386和32位MIPS上,通过原子函数访问64位字(类型Int64和Uint64自动对齐)的调用方有责任安排64位对齐。可以依靠分配的结构体,数组或切片中的第一个字,全局变量中的第一个字或局部变量中的第一个字(因为所有原子操作的主题都将逃逸到堆中)为64位对齐。

最后修改 October 10, 2024: 更新 (a4b8f85)