目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "sync/atomic"

  • 概观

  • 索引

  • 示例

概观

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

这些功能需要非常小心才能正确使用。除特殊的低级别应用程序外,同步更适合使用频道或同步软件包的功能。通过沟通共享内存; 不要通过共享内存进行通信。

由 SwapT 函数实现的交换操作是以下原子等值:

old = *addr*addr = newreturn old

由 CompareAndSwapT 函数实现的比较和交换操作与以下原子等价:

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

由 Add T函数实现的 add 操作与以下操作相当:

*addr += deltareturn *addr

由 LoadT 和 StoreT 函数实现的加载和存储操作是  "return *addr" and "*addr = val" 的原子等价物。

索引

  • 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{})

  • 错误

示例

Value (Config) Value (ReadMostly)

打包文件

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 自动向 delta addr 添加 delta 并返回新值。

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 对不安全的指针值执行比较和交换操作。

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

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

SwapInt32 将自动地新成员存储到 * addr 并返回以前的 * addr 值。

func SwapInt64

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

SwapInt64 自动地将新的值存储到 * addr 并返回前一个 * addr 值。

func SwapPointer

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

SwapPointer 自动地将新的值存储到 * addr 并返回以前的 * addr 值。

func SwapUint32

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

SwapUint32 自动地将新的值存储到 * addr 并返回前一个 * addr 值。

func SwapUint64

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

SwapUint64 自动地将新的值存储到 * addr 中,并返回以前的 * addr 值。

func SwapUintptr

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

SwapUintptr 自动地将新值存储到 * addr 中,并返回前一个 * addr 值。

type Value

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

首次使用后不得复制 Value 。

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

示例(配置)

以下示例显示如何使用 Value 进行周期性程序配置更新以及将更改传播到工作程序。

编码:

var config 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                }        }()}

示例(ReadMostly)

以下示例说明如何使用写入时复制习惯用法维护可扩展的经常读取但不经常更新的数据结构。

编码:

type Map map[string]stringvar m Value
m.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 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

func (*Value) Load

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

Load 返回最近的存储设置的值。如果此值没有存储调用,则返回 nil 。

func (*Value) Store

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

Store 将 Value 的值设置为 x 。对于给定值的所有对 Store的调用都必须使用相同具体类型的值。存储不一致的类型恐慌,就像 Store(nil) 一样。

错误

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

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

在 ARM 和 x86-32 上,调用者都有责任安排自动访问64位字的64位对齐方式。变量或分配的结构,数组或片中的第一个字可以依赖于64位对齐。

上一篇: 下一篇: