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

在Go语言中,atomic包提供了对基本数据类型的原子操作支持,用于在并发环境下安全地读取、写入、修改共享变量,而无需使用互斥锁(
sync.Mutex
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
<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
}
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)
}
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
}
注意:上面例子存在ABA问题风险,生产环境建议结合
sync.Once
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))
}
atomic.Value
int64
atomic
sync.Mutex
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)
}
注意:
atomic.Value
基本上就这些。atomic包是Go并发编程中高效、底层的工具,掌握它能写出更轻量、高性能的并发代码。关键在于理解每种操作的语义和适用场景,避免误用导致竞态条件。不复杂但容易忽略细节。
以上就是Golang原子操作详解 atomic包函数使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号