头图

Introduction to sync/atomic

When we want to modify a variable concurrently and safely, in addition to using the official mutex , we can also use the atomic operation of the sync/atomic package, which can ensure that the variable is not read or modified by other coroutines during the period. Affected.

atomic operation of the atomic package is implemented by CPU instructions, which is implemented at the hardware level. The performance is better and there is no need to record many states mutex Of course, mutex not only the concurrency control of variables, but also the concurrency control of code blocks. The two focuses are different.

sync/atomic operation

The atomic package has several atomic operations, mainly Add, CompareAndSwap, Load, Store, and Swap.

Add

The atomic Add is for atomic addition of int and uint:

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 compare and exchange method implements a function similar to optimistic locking, only the original value is the same as the old value passed in, it will be modified:

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 may produce ABA phenomenon. That is, the original value is A, and it is modified to B later, and then modified to A later. In this case, the CompareAndSwap rule is also complied with, even if it has been changed midway.

Load

The Load method is to prevent other coroutines from initiating modification actions during the reading process, which affects the reading results. It is often used for the entire reading of the configuration item

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

If there is an atomic read, there will be an atomic modification of the value. The Add mentioned above is only applicable to the increase and decrease of int and uint types, and there is no other type of modification. The Sotre method achieves the correctness through atomic modification of the unsafe.Pointer pointer. 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

The Swap method realizes the atomic exchange of values, not only int and uint can be exchanged, but also pointers.

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)

Summarize

Atomic may not be used in many cases. After all, mutex is more extensible and friendly to use. But this does not prevent us from pursuing the ultimate performance, sometimes, the details determine the performance!


interested in 16126690a62ebc can search the public account "Read New Technology" to follow more push articles.
you can, just like, leave a comment, share, thank you for your support!
Read new technology, read more new knowledge.
阅新技术


lincoln
57 声望12 粉丝

分享有深度、有启示的技术文章;