# package atomic
`import "sync/atomic"`
atomic包提供了底层的原子级内存操作,对于同步算法的实现很有用。
这些函数必须谨慎地保证正确使用。除了某些特殊的底层应用,使用通道或者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 LoadInt32(addr \*int32) (val int32)](#LoadInt32)
* [func LoadInt64(addr \*int64) (val int64)](#LoadInt64)
* [func LoadUint32(addr \*uint32) (val uint32)](#LoadUint32)
* [func LoadUint64(addr \*uint64) (val uint64)](#LoadUint64)
* [func LoadUintptr(addr \*uintptr) (val uintptr)](#LoadUintptr)
* [func LoadPointer(addr \*unsafe.Pointer) (val unsafe.Pointer)](#LoadPointer)
* [func StoreInt32(addr \*int32, val int32)](#StoreInt32)
* [func StoreInt64(addr \*int64, val int64)](#StoreInt64)
* [func StoreUint32(addr \*uint32, val uint32)](#StoreUint32)
* [func StoreUint64(addr \*uint64, val uint64)](#StoreUint64)
* [func StoreUintptr(addr \*uintptr, val uintptr)](#StoreUintptr)
* [func StorePointer(addr \*unsafe.Pointer, val unsafe.Pointer)](#StorePointer)
* [func AddInt32(addr \*int32, delta int32) (new int32)](#AddInt32)
* [func AddInt64(addr \*int64, delta int64) (new int64)](#AddInt64)
* [func AddUint32(addr \*uint32, delta uint32) (new uint32)](#AddUint32)
* [func AddUint64(addr \*uint64, delta uint64) (new uint64)](#AddUint64)
* [func AddUintptr(addr \*uintptr, delta uintptr) (new uintptr)](#AddUintptr)
* [func SwapInt32(addr \*int32, new int32) (old int32)](#SwapInt32)
* [func SwapInt64(addr \*int64, new int64) (old int64)](#SwapInt64)
* [func SwapUint32(addr \*uint32, new uint32) (old uint32)](#SwapUint32)
* [func SwapUint64(addr \*uint64, new uint64) (old uint64)](#SwapUint64)
* [func SwapUintptr(addr \*uintptr, new uintptr) (old uintptr)](#SwapUintptr)
* [func SwapPointer(addr \*unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)](#SwapPointer)
* [func CompareAndSwapInt32(addr \*int32, old, new int32) (swapped bool)](#CompareAndSwapInt32)
* [func CompareAndSwapInt64(addr \*int64, old, new int64) (swapped bool)](#CompareAndSwapInt64)
* [func CompareAndSwapUint32(addr \*uint32, old, new uint32) (swapped bool)](#CompareAndSwapUint32)
* [func CompareAndSwapUint64(addr \*uint64, old, new uint64) (swapped bool)](#CompareAndSwapUint64)
* [func CompareAndSwapUintptr(addr \*uintptr, old, new uintptr) (swapped bool)](#CompareAndSwapUintptr)
* [func CompareAndSwapPointer(addr \*unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)](#CompareAndSwapPointer)
## func [LoadInt32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L113 "View Source")
```
func LoadInt32(addr *int32) (val int32)
```
LoadInt32原子性的获取\*addr的值。
## func [LoadInt64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L116 "View Source")
```
func LoadInt64(addr *int64) (val int64)
```
LoadInt64原子性的获取\*addr的值。
## func [LoadUint32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L119 "View Source")
```
func LoadUint32(addr *uint32) (val uint32)
```
LoadUint32原子性的获取\*addr的值。
## func [LoadUint64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L122 "View Source")
```
func LoadUint64(addr *uint64) (val uint64)
```
LoadUint64原子性的获取\*addr的值。
## func [LoadUintptr](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L125 "View Source")
```
func LoadUintptr(addr *uintptr) (val uintptr)
```
LoadUintptr原子性的获取\*addr的值。
## func [LoadPointer](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L128 "View Source")
```
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
```
LoadPointer原子性的获取\*addr的值。
## func [StoreInt32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L131 "View Source")
```
func StoreInt32(addr *int32, val int32)
```
StoreInt32原子性的将val的值保存到\*addr。
## func [StoreInt64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L134 "View Source")
```
func StoreInt64(addr *int64, val int64)
```
StoreInt64原子性的将val的值保存到\*addr。
## func [StoreUint32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L137 "View Source")
```
func StoreUint32(addr *uint32, val uint32)
```
StoreUint32原子性的将val的值保存到\*addr。
## func [StoreUint64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L140 "View Source")
```
func StoreUint64(addr *uint64, val uint64)
```
StoreUint64原子性的将val的值保存到\*addr。
## func [StoreUintptr](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L143 "View Source")
```
func StoreUintptr(addr *uintptr, val uintptr)
```
StoreUintptr原子性的将val的值保存到\*addr。
## func [StorePointer](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L146 "View Source")
```
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
```
StorePointer原子性的将val的值保存到\*addr。
## func [AddInt32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L94 "View Source")
```
func AddInt32(addr *int32, delta int32) (new int32)
```
AddInt32原子性的将val的值添加到\*addr并返回新值。
## func [AddInt64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L102 "View Source")
```
func AddInt64(addr *int64, delta int64) (new int64)
```
AddInt64原子性的将val的值添加到\*addr并返回新值。
## func [AddUint32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L99 "View Source")
```
func AddUint32(addr *uint32, delta uint32) (new uint32)
```
AddUint32原子性的将val的值添加到\*addr并返回新值。
如要减去一个值c,调用AddUint32(&x, ^uint32(c-1));特别的,让x减1,调用AddUint32(&x, ^uint32(0))。
## func [AddUint64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L107 "View Source")
```
func AddUint64(addr *uint64, delta uint64) (new uint64)
```
AddUint64原子性的将val的值添加到\*addr并返回新值。
如要减去一个值c,调用AddUint64(&x, ^uint64(c-1));特别的,让x减1,调用AddUint64(&x, ^uint64(0))。
## func [AddUintptr](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L110 "View Source")
```
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
```
AddUintptr原子性的将val的值添加到\*addr并返回新值。
## func [SwapInt32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L58 "View Source")
```
func SwapInt32(addr *int32, new int32) (old int32)
```
SwapInt32原子性的将新值保存到\*addr并返回旧值。
## func [SwapInt64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L61 "View Source")
```
func SwapInt64(addr *int64, new int64) (old int64)
```
SwapInt64原子性的将新值保存到\*addr并返回旧值。
## func [SwapUint32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L64 "View Source")
```
func SwapUint32(addr *uint32, new uint32) (old uint32)
```
SwapUint32原子性的将新值保存到\*addr并返回旧值。
## func [SwapUint64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L67 "View Source")
```
func SwapUint64(addr *uint64, new uint64) (old uint64)
```
SwapUint64原子性的将新值保存到\*addr并返回旧值。
## func [SwapUintptr](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L70 "View Source")
```
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)
```
SwapUintptr原子性的将新值保存到\*addr并返回旧值。
## func [SwapPointer](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L73 "View Source")
```
func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
```
SwapPointer原子性的将新值保存到\*addr并返回旧值。
## func [CompareAndSwapInt32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L76 "View Source")
```
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
```
CompareAndSwapInt32原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## func [CompareAndSwapInt64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L79 "View Source")
```
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
```
CompareAndSwapInt64原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## func [CompareAndSwapUint32](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L82 "View Source")
```
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
```
CompareAndSwapUint32原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## func [CompareAndSwapUint64](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L85 "View Source")
```
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
```
CompareAndSwapUint64原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## func [CompareAndSwapUintptr](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L88 "View Source")
```
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
```
CompareAndSwapUintptr原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## func [CompareAndSwapPointer](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L91 "View Source")
```
func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)
```
CompareAndSwapPointer原子性的比较\*addr和old,如果相同则将new赋值给\*addr并返回真。
## Bugs
[☞](https://github.com/golang/go/blob/master/src/sync/atomic/doc.go#L48 "View Source") 在x86-32构架芯片上,64位函数使用的指令在Pentium MMX之前是不可用的;在非Linux的ARM芯片上,64位函数使用的指令在ARMv6k core之前是不可用的。不管是ARM又或x86-32芯片,安排原子性访问的64位word的64位对齐都是调用者的责任。可以依靠全局变量或申请的切片/结构体的第一个word来实现64位对齐。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe