当前位置:网站首页>Explanation of atomic operation in golang concurrent programming

Explanation of atomic operation in golang concurrent programming

2022-06-22 03:23:00 ManNiaoQinFen

atomic The atomic operation provided ensures that there is only one... At any one time goroutine Operate on variables , Make good use of atomic It can avoid a large number of lock operations in the program .

atomic Common operations are :
Increase or decrease
load read
Compare and exchange cas
In exchange for
Storage write
These operations are described below .

Increase or decrease operation

atomic The package provides the following to Add Add or remove prefix :

- func AddInt32(addr *int32, delta int32) (new int32)
- func AddInt64(addr *int64, delta int64) (new int64)
- func AddUint32(addr *uint32, delta uint32) (new uint32)
- func AddUint64(addr *uint64, delta uint64) (new uint64)
- func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)

Load operation

atomic The package provides the following to Load Add or remove prefix :

- func LoadInt32(addr *int32) (val int32)
- func LoadInt64(addr *int64) (val int64)
- func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
- func LoadUint32(addr *uint32) (val uint32)
- func LoadUint64(addr *uint64) (val uint64)
- func LoadUintptr(addr *uintptr) (val uintptr)

The loading operation can guarantee the value of the atomic read variable , When reading , Any other CPU No operation can read or write this variable , Its implementation mechanism is supported by the underlying hardware .

Compare and exchange

This operation is abbreviated to CAS(Compare And Swap). The prefix for this type of operation is CompareAndSwap :

- func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
- func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
- func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (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)

This operation first ensures that the value of the variable has not been changed before switching , That is, the parameters are still maintained old Recorded value , Only when this premise is met can the exchange operation be carried out .CAS The approach is similar to the common optimistic locking mechanism when operating a database

In exchange for

This type of operation is prefixed with Swap:

- func SwapInt32(addr *int32, new int32) (old int32)
- func SwapInt64(addr *int64, new int64) (old int64)
- func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
- func SwapUint32(addr *uint32, new uint32) (old uint32)
- func SwapUint64(addr *uint64, new uint64) (old uint64)
- func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)

be relative to CAS, Obviously, such operations are more violent and direct , Regardless of whether the old value of the variable has been changed , Directly assign a new value and then return the back substituted value .

Storage

This type of operation is prefixed with Store:

- func StoreInt32(addr *int32, val int32)
- func StoreInt64(addr *int64, val int64)
- func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
- func StoreUint32(addr *uint32, val uint32)
- func StoreUint64(addr *uint64, val uint64)
- func StoreUintptr(addr *uintptr, val uintptr)

This kind of operation ensures the atomicity of writing variables , Avoid other operations from reading dirty data in the process of modifying variables .

原网站

版权声明
本文为[ManNiaoQinFen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220308321795.html