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

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

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

在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 int64

func increment() {atomic.AddInt64(&counter, 1)}

func main() {var wg sync.WaitGroupfor 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 = 1

func 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 int32var config *Config

func 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.Pointer

func 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.Value

func init() {config.Store(&Config{Version: "v1"})}

func updateConfig(newCfg *Config) {config.Store(newCfg)}

func getCurrent() Config {return config.Load().(Config)}

注意:

atomic.Value

一旦使用,就不能复制,且读写必须是相同类型。

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

以上就是Golang原子操作详解 atomic包函数使用的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1401312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang reflect反射机制 动态类型检查
上一篇 2025年12月15日 17:43:45
Golang代码组织规范 包划分与命名
下一篇 2025年12月15日 17:43:57

相关推荐

发表回复

登录后才能评论
关注微信