Package atomic

import "sync/atomic"
Overview
Index
Examples

Overview ▾

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

这些功能需要格外小心才能正确使用. 除特殊的低层应用程序外,最好使用通道或sync软件包的功能来完成同步. 通过通信共享内存; 不要通过共享内存进行通信.

由SwapT函数实现的交换操作在原子上等效于:

old = *addr
*addr = new
return old

由CompareAndSwapT函数实现的比较交换操作在原子上等效于:

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

由AddT函数实现的加法操作在原子上等效于:

*addr += delta
return *addr

由LoadT和StoreT函数实现的加载和存储操作是" return * addr"和" * addr = val"的原子等效项.

Index ▾

func AddInt32(addr *int32, delta int32) (new int32)
func AddInt64(addr *int64, delta int64) (new int64)
func AddUint32(addr *uint32, delta uint32) (new uint32)
func AddUint64(addr *uint64, delta uint64) (new uint64)
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
func LoadInt32(addr *int32) (val int32)
func LoadInt64(addr *int64) (val int64)
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
func LoadUint32(addr *uint32) (val uint32)
func LoadUint64(addr *uint64) (val uint64)
func LoadUintptr(addr *uintptr) (val uintptr)
func StoreInt32(addr *int32, val int32)
func StoreInt64(addr *int64, val int64)
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
func StoreUint32(addr *uint32, val uint32)
func StoreUint64(addr *uint64, val uint64)
func StoreUintptr(addr *uintptr, val uintptr)
func SwapInt32(addr *int32, new int32) (old int32)
func SwapInt64(addr *int64, new int64) (old int64)
func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
func SwapUint32(addr *uint32, new uint32) (old uint32)
func SwapUint64(addr *uint64, new uint64) (old uint64)
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)
type Value
    func (v *Value) Load() (x interface{})
    func (v *Value) Store(x interface{})
Bugs

Examples

Value (Config)
Value (ReadMostly)

Package files

doc.go value.go

func AddInt32

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

AddInt32以原子方式将增量添加到* addr并返回新值.

func AddInt64

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

AddInt64以原子方式将增量添加到* addr并返回新值.

func AddUint32

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

AddUint32以原子方式将增量添加到* addr并返回新值. 要从x减去带符号的正常数值c,请执行AddUint32(&x,^ uint32(c-1)). 特别是要减少x,请执行AddUint32(&x,^ uint32(0)).

func AddUint64

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

AddUint64以原子方式将增量添加到* addr并返回新值. 要从x减去带符号的正常数值c,请执行AddUint64(&x,^ uint64(c-1)). 特别是要减少x,请执行AddUint64(&x,^ uint64(0)).

func AddUintptr

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

AddUintptr自动将增量添加到* addr并返回新值.

func CompareAndSwapInt32

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

CompareAndSwapInt32对int32值执行比较和交换操作.

func CompareAndSwapInt64

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

CompareAndSwapInt64对int64值执行比较和交换操作.

func CompareAndSwapPointer

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

CompareAndSwapPointer对不安全的Pointer值执行比较和交换操作.

func CompareAndSwapUint32

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

CompareAndSwapUint32对uint32值执行比较和交换操作.

func CompareAndSwapUint64

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

CompareAndSwapUint64对uint64值执行比较和交换操作.

func CompareAndSwapUintptr

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

CompareAndSwapUintptr对uintptr值执行比较和交换操作.

func LoadInt32

func LoadInt32(addr *int32) (val int32)

LoadInt32自动加载* addr.

func LoadInt64

func LoadInt64(addr *int64) (val int64)

LoadInt64自动加载* addr.

func LoadPointer

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

LoadPointer atomically loads *addr.

func LoadUint32

func LoadUint32(addr *uint32) (val uint32)

LoadUint32自动加载* addr.

func LoadUint64

func LoadUint64(addr *uint64) (val uint64)

LoadUint64自动加载* addr.

func LoadUintptr

func LoadUintptr(addr *uintptr) (val uintptr)

LoadUintptr自动加载* addr.

func StoreInt32

func StoreInt32(addr *int32, val int32)

StoreInt32将val原子存储到* addr中.

func StoreInt64

func StoreInt64(addr *int64, val int64)

StoreInt64将val原子存储到* addr中.

func StorePointer

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

StorePointer原子地将val存储到* addr中.

func StoreUint32

func StoreUint32(addr *uint32, val uint32)

StoreUint32将val原子存储到* addr中.

func StoreUint64

func StoreUint64(addr *uint64, val uint64)

StoreUint64将val原子存储到* addr中.

func StoreUintptr

func StoreUintptr(addr *uintptr, val uintptr)

StoreUintptr原子地将val存储到* addr中.

func SwapInt32 1.2

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

SwapInt32以原子方式将new存储到* addr中,并返回先前的* addr值.

func SwapInt64 1.2

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

SwapInt64以原子方式将new存储到* addr中,并返回先前的* addr值.

func SwapPointer 1.2

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

SwapPointer自动将new存储到* addr中,并返回先前的* addr值.

func SwapUint32 1.2

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

SwapUint32以原子方式将new存储到* addr中,并返回先前的* addr值.

func SwapUint64 1.2

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

SwapUint64以原子方式将new存储到* addr中,并返回先前的* addr值.

func SwapUintptr 1.2

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

SwapUintptr自动将new存储到* addr中,并返回先前的* addr值.

type Value 1.4

值提供原子负载并存储一致类型的值. 值的零值从Load返回零. 调用存储后,不得复制值.

首次使用后不得复制值.

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

示例(配置)

以下示例显示了如何使用Value进行定期程序配置更新以及将更改传播到工作程序goroutine.

示例(只读)

下面的示例说明如何使用写时复制习惯来维护可伸缩的,频繁读取但不经常更新的数据结构.

func (*Value) Load 1.4

func (v *Value) Load() (x interface{})

加载返回由最新商店设置的值. 如果没有针对此值的存储调用,则返回nil.

func (*Value) Store 1.4

func (v *Value) Store(x interface{})

Store将"值"的值设置为x. 给定值的所有Store调用都必须使用相同具体类型的值. 不一致类型的panic存储,Store(nil)也是如此.

Bugs

by  ICOPY.SITE