登录
首页 >  Golang >  Go教程

Golang原子操作详解与atomic包使用

时间:2026-04-11 16:49:33 413浏览 收藏

Go语言的atomic包为并发编程提供了高效、无锁的原子操作支持,涵盖整型增减、指针/值的原子加载与存储、比较并交换(CAS)及交换等核心能力,特别适用于计数器、状态标志、单例初始化和配置热更新等轻量级共享状态场景;它规避了互斥锁的开销,但需严格遵循使用规范——如禁止混合普通读写、注意32位系统下int64非原子性、避免复制atomic.Value等,掌握这些细节才能写出既高性能又线程安全的Go代码。

Golang原子操作详解 atomic包函数使用

在Go语言中,atomic包提供了对基本数据类型的原子操作支持,用于在并发环境下安全地读取、写入、修改共享变量,而无需使用互斥锁(sync.Mutex)。原子操作效率更高,适用于简单的共享状态管理,比如计数器、标志位等场景。

atomic包常用函数分类

atomic包主要支持对整型(int32int64)、指针、uint32uint64uintptrbool 类型的原子操作。以下是核心函数分类及使用方式:

1. 原子增减(Add)

用于对整型变量进行原子加减操作:

  • 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 int64
<p>func increment() {
atomic.AddInt64(&counter, 1)
}</p><p>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
}
</p>

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 = 1
<p>func monitor() {
for {
if atomic.LoadInt32(&running) == 0 {
fmt.Println("Stopping...")
return
}
time.Sleep(100 * time.Millisecond)
}
}</p><p>func main() {
go monitor()
time.Sleep(2 <em> time.Second)
atomic.StoreInt32(&running, 0)
time.Sleep(100 </em> time.Millisecond)
}
</p>

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 *Config
<p>func GetConfig() <em>Config {
if atomic.LoadInt32(&initialized) == 0 {
atomic.CompareAndSwapInt32(&initialized, 0, 1)
config = &Config{ /</em> 初始化 */ }
}
return config
}
</p>

注意:上面例子存在ABA问题风险,生产环境建议结合sync.Once或使用指针CAS更安全。

4. 交换操作(Swap)

原子地将新值写入变量,并返回旧值。

  • atomic.SwapInt32(&val, new)
  • atomic.SwapInt64(&val, new)
  • atomic.SwapPointer(&ptr, new)

示例:切换配置指针

var configPtr unsafe.Pointer
<p>func updateConfig(newConfig *Config) {
atomic.SwapPointer(&configPtr, unsafe.Pointer(newConfig))
}</p><p>func getCurrentConfig() <em>Config {
return (</em>Config)(atomic.LoadPointer(&configPtr))
}
</p>

使用注意事项

  • 原子操作只适用于基本类型,不能用于结构体整体(除非是atomic.Value
  • 必须对变量地址操作,不能传值
  • int64在32位系统上操作不是原子的,必须使用atomic
  • 避免混合使用原子操作和普通读写,会导致数据竞争
  • 复杂逻辑建议使用sync.Mutex,原子操作适合轻量级场景

atomic.Value:任意类型的原子操作

Go还提供atomic.Value类型,可用于存储任意类型的值(需运行时确定),常用于配置热更新。

var config atomic.Value
<p>func init() {
config.Store(&Config{Version: "v1"})
}</p><p>func updateConfig(newCfg *Config) {
config.Store(newCfg)
}</p><p>func getCurrent() <em>Config {
return config.Load().(</em>Config)
}
</p>

注意:atomic.Value一旦使用,就不能复制,且读写必须是相同类型。

基本上就这些。atomic包是Go并发编程中高效、底层的工具,掌握它能写出更轻量、高性能的并发代码。关键在于理解每种操作的语义和适用场景,避免误用导致竞态条件。不复杂但容易忽略细节。

今天关于《Golang原子操作详解与atomic包使用》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>