当前位置:网站首页>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 .
 Read new technology

原网站

版权声明
本文为[yue_ xin_ tech]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231438381001.html