Go的atomic包提供整型、指针等类型的原子操作,支持增减(Add)、加载存储(Load/Store)、比较并交换(CAS)和交换(Swap),适用于并发下计数器、标志位等轻量场景,避免锁开销。示例包括原子计数、状态控制、单例初始化与配置更新,需注意32位系统int64非原子、禁止混合普通读写及atomic.Value不可复制等问题。

在Go语言中,atomic包提供了对基本数据类型的原子操作支持,用于在并发环境下安全地读取、写入、修改共享变量,而无需使用互斥锁(
sync.Mutex)。原子操作效率更高,适用于简单的共享状态管理,比如计数器、标志位等场景。
atomic包常用函数分类
atomic包主要支持对整型(
int32、
int64)、指针、
uint32、
uint64、
uintptr和
bool类型的原子操作。以下是核心函数分类及使用方式:
1. 原子增减(Add)
用于对整型变量进行原子加减操作:
立即学习“go语言免费学习笔记(深入)”;
atomic.AddInt32(&val, delta)
:对int32
变量加delta
atomic.AddInt64(&val, delta)
:对int64
变量加delta
atomic.AddUint32(&val, delta)
:对uint32
加atomic.AddUint64(&val, delta)
:对uint64
加atomic.AddUintptr
:用于指针偏移,较少使用
示例:实现一个并发安全的计数器
var counter int64func increment() { atomic.AddInt64(&counter, 1) }
func main() { var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) go func() { defer wg.Done() increment() }() } wg.Wait() fmt.Println("Counter:", atomic.LoadInt64(&counter)) // 1000 }
2. 原子加载与存储(Load / Store)
用于安全地读取和写入变量值,避免并发读写导致的数据竞争。
atomic.LoadInt32(&val)
:原子读取int32
atomic.LoadInt64(&val)
:原子读取int64
atomic.LoadUint32(&val)
:原子读取uint32
atomic.LoadPointer(&ptr)
:原子读取指针atomic.StoreInt32(&val, new)
:原子写入int32
atomic.StoreInt64(&val, new)
:原子写入int64
注意:所有Load和Store操作都必须传入变量地址。
示例:用原子操作控制程序运行状态
var running int32 = 1func monitor() { for { if atomic.LoadInt32(&running) == 0 { fmt.Println("Stopping...") return } time.Sleep(100 * time.Millisecond) } }
func main() { go monitor() time.Sleep(2 time.Second) atomic.StoreInt32(&running, 0) time.Sleep(100 time.Millisecond) }
3. 比较并交换(Compare And Swap, CAS)
CAS是实现无锁算法的核心,只有当当前值等于旧值时,才将新值写入。
atomic.CompareAndSwapInt32(&val, old, new)
atomic.CompareAndSwapInt64(&val, old, new)
atomic.CompareAndSwapUint32(&val, old, new)
atomic.CompareAndSwapPointer(&ptr, old, new)
返回
bool,表示是否交换成功。
示例:实现线程安全的单例初始化
var initialized int32 var config *Configfunc GetConfig() Config { if atomic.LoadInt32(&initialized) == 0 { atomic.CompareAndSwapInt32(&initialized, 0, 1) config = &Config{ / 初始化 */ } } return config }
注意:上面例子存在ABA问题风险,生产环境建议结合
sync.Once或使用指针CAS更安全。
4. 交换操作(Swap)
原子地将新值写入变量,并返回旧值。
atomic.SwapInt32(&val, new)
atomic.SwapInt64(&val, new)
atomic.SwapPointer(&ptr, new)
示例:切换配置指针
var configPtr unsafe.Pointerfunc updateConfig(newConfig *Config) { atomic.SwapPointer(&configPtr, unsafe.Pointer(newConfig)) }
func getCurrentConfig() Config { return (Config)(atomic.LoadPointer(&configPtr)) }
使用注意事项
- 原子操作只适用于基本类型,不能用于结构体整体(除非是
atomic.Value
) - 必须对变量地址操作,不能传值
int64
在32位系统上操作不是原子的,必须使用atomic
包- 避免混合使用原子操作和普通读写,会导致数据竞争
- 复杂逻辑建议使用
sync.Mutex
,原子操作适合轻量级场景
atomic.Value:任意类型的原子操作
Go还提供
atomic.Value类型,可用于存储任意类型的值(需运行时确定),常用于配置热更新。
var config atomic.Valuefunc init() { config.Store(&Config{Version: "v1"}) }
func updateConfig(newCfg *Config) { config.Store(newCfg) }
func getCurrent() Config { return config.Load().(Config) }
注意:
atomic.Value一旦使用,就不能复制,且读写必须是相同类型。
基本上就这些。atomic包是Go并发编程中高效、底层的工具,掌握它能写出更轻量、高性能的并发代码。关键在于理解每种操作的语义和适用场景,避免误用导致竞态条件。不复杂但容易忽略细节。










