当前位置:网站首页>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 .
边栏推荐
- On the auto increment of int type primary key in MySQL
- 基于Pytorch的图像分类总结:Swin Transformer
- uv_loop_init()流程
- Project management software development project management
- Fastdfs-5.0.5 installation
- [nvme2.0b 8] nvme queue arbitration mechanism
- 【NVMe2.0b 12】NVM 容量模型
- 【leetcode周赛总结】LeetCode第298场周赛总结(6.19)
- Use the serialize common command
- libuv异步任务逻辑和uv_queue_work()
猜你喜欢

调度功能:splunk-operator-controller-manager

AtCoder Regular Contest 142

什么是SSL证书,拥有一个SSL证书有什么好处?

【NVMe2.0b 5】NVM Subsystem

Lectures explanation for unsupervised graph level representation learning (usib)

从根儿上理解虚拟内存

【NVMe2.0b 8】NVMe 队列仲裁机制

为什么程序员写的第一个程序是“Hello World!”

Typora + picGo 配置图床实现图片自动上传

Tag dynamic programming - preliminary knowledge for question brushing -1 Dynamic programming five part problem solving method + lt.509 Fibonacci number / Sword finger offer 10 I + lt.70. Climbing stai
随机推荐
AtCoder Beginner Contest 252(dijkstra,逆向思维)
Factory mode
Redis configuration and optimization
eu5,eu7,ex3,ex5安装第三方app
Operating instructions for tcp202 current probe of Tektronix oscilloscope
360edr planing
unity3D C# 在区间内生成不重复的随机数
Implementation of epoll+threadpool high concurrency network IO model
Implementation principle and application practice of Flink CDC mongodb connector
使用开源软件攒一个企业级图数据平台解决方案
tail的用法
linux下安装mysql8及使用(转载)
Figure data platform solution: cluster deployment
BOM 属性、方法、事件应用案例
微信小程序onPageScroll无效
[nvme2.0b 9] controller initialization process
工厂模式
关于Map做状态映射的问题
NXP imx8mp学习记录
uv_loop_init()流程