菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
342
0

Golang atomic

原创
05/13 14:22
阅读数 62941

原子操作函数

分为下面系列函数,其中Xxx可以是Int32/Int64/Uint32/Uint64/Uintptr/Pointer其中一种。

 

1.SwapXxx系列:交换新旧值;

// SwapInt32 atomically stores new into *addr and returns the previous *addr value.
func SwapInt32(addr *int32, new int32) (old int32)

 

2.CompareAndSwapXxx系列:比较并交换;

// CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)

 

3.AddXxx系列:加减;

// AddInt32 atomically adds delta to *addr and returns the new value.
func AddInt32(addr *int32, delta int32) (new int32)

 

4.LoadXxx系列:读取;

// LoadInt32 atomically loads *addr.
func LoadInt32(addr *int32) (val int32)

 

5.StoreXxx系列:保存;

// StoreInt32 atomically stores val into *addr.
func StoreInt32(addr *int32, val int32)

 

以上。

 

发表评论

0/200
342 点赞
0 评论
收藏