当前位置:网站首页>Important knowledge of golang: atomic atomic operation
Important knowledge of golang: atomic atomic operation
2022-06-23 15:24:00 【yue_ xin_ tech】
sync/atomic Introduce
When we want to make concurrent and safe changes to a variable , In addition to using official mutex, You can also use sync/atomic The atomic operation of the package , It can ensure that the reading or modification of variables is not affected by other processes .
atomic Bag Atomic manipulation It's through CPU Instructions , That is, it is implemented at the hardware level , Good performance , No need to be like mutex Record a lot of states like that . Of course ,mutex Not just concurrency control of variables , More is the concurrency control of code blocks ,2 The focus is different .
sync/atomic operation
atomic There are several atomic operations , Mainly Add、CompareAndSwap、Load、Store、Swap.
Add
atomic Of Add Is aimed at int and uint To add atomic value :
func AddInt32(addr *int32, delta int32) (new int32)
func AddUint32(addr *uint32, delta uint32) (new uint32)
func AddInt64(addr *int64, delta int64) (new int64)
func AddUint64(addr *uint64, delta uint64) (new uint64)
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
CompareAndSwap
The comparison and exchange method implements a function similar to optimistic lock , Only the original value and the passed in old Have the same value , Will modify :
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)
It should be noted that ,CompareAndSwap It is possible to produce ABA Phenomenon occurs . That is, the original value is A, Later modified B, Later, it is modified to A. In this case, it also conforms to CompareAndSwap The rules , Even if it has been changed halfway .
Load
Load The method is to prevent during reading , Other processes initiate modification actions , Affect the reading results , Commonly used in Configuration item The entire read .
func LoadInt32(addr *int32) (val int32)
func LoadInt64(addr *int64) (val int64)
func LoadUint32(addr *uint32) (val uint32)
func LoadUint64(addr *uint64) (val uint64)
func LoadUintptr(addr *uintptr) (val uintptr)
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
Store
There are atoms reading , There are atomic modification values , As mentioned earlier Add Only applicable to int、uint Increase or decrease of type , There are no other types of modifications , and Sotre Methods by unsafe.Pointer Pointer atom modification , To achieve other types of modifications .
func StoreInt32(addr *int32, val int32)
func StoreInt64(addr *int64, val int64)
func StoreUint32(addr *uint32, val uint32)
func StoreUint64(addr *uint64, val uint64)
func StoreUintptr(addr *uintptr, val uintptr)
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
Swap
Swap Method realizes the atomic exchange of pairs of values , Not only int,uint You can exchange , The pointer can also .
func SwapInt32(addr *int32, new int32) (old int32)
func SwapInt64(addr *int64, new int64) (old int64)
func SwapUint32(addr *uint32, new uint32) (old uint32)
func SwapUint64(addr *uint64, new uint64) (old uint64)
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)
func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
summary
atomic Many times it may not be used , After all mutex The expansibility of is quite good , It is also friendly to use . But this does not hinder our pursuit of ultimate performance , occasionally , Details determine performance !
Interested friends can search the official account 「 Read new technology 」, Pay attention to more push articles .
If you can , Just like it by the way 、 Leave a message 、 Under the share , Thank you for your support !
Read new technology , Read more new knowledge .
边栏推荐
猜你喜欢

小米为何深陷芯片泥潭?

2021-05-22

Half wave loss equal thickness and equal inclination interference

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of the TCC mode

js遍历数组(用forEach()方法)

2021-05-08
![[普通物理] 光的衍射](/img/1a/20dbd15e0c8c91a3e59753b2f6797a.png)
[普通物理] 光的衍射

百万奖金等你来拿,首届中国元宇宙创新应用大赛联合创业黑马火热招募中!

变压器只能转换交流电,那直流电怎么转换呢?

A transformer can only convert alternating current. How can I convert direct current?
随机推荐
Summary of operating system underlying knowledge (interview)
[普通物理] 光的衍射
mysql主从只同步部分库或表的思路与方法
Explain in detail the principle and implementation of redis distributed lock
2021-05-08
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of the TCC mode
2021-06-07
golang 重要知识:定时器 timer
2021-05-08
Volatile~ variables are not visible under multithreading
Logistics trade related
Converging ecology, enabling safe operation, Huawei cloud security, cloud brain intelligent service security
F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
【云驻共创】制造业企业如何建设“条码工厂”
[opencv450] salt and pepper noise demo
WebService interface publishing and calling
[cloud based co creation] intelligent supply chain plan: improve the decision-making level of the supply chain and help enterprises reduce costs and increase efficiency
Half wave loss equal thickness and equal inclination interference
Sectigo(Comodo)证书的由来