当前位置:网站首页>golang并发编程之原子操作详解
golang并发编程之原子操作详解
2022-06-22 03:08:00 【ManNiaoQinFen】
atomic 提供的原子操作能够确保任一时刻只有一个goroutine对变量进行操作,善用 atomic 能够避免程序中出现大量的锁操作。
atomic常见操作有:
增减
载入 read
比较并交换 cas
交换
存储 write
下面将分别介绍这些操作。
增减操作
atomic 包中提供了如下以Add为前缀的增减操作:
- 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)
载入操作
atomic 包中提供了如下以Load为前缀的增减操作:
- 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)
载入操作能够保证原子的读变量的值,当读取的时候,任何其他CPU操作都无法对该变量进行读写,其实现机制受到底层硬件的支持。
比较并交换
该操作简称 CAS(Compare And Swap)。 这类操作的前缀为 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)
该操作在进行交换前首先确保变量的值未被更改,即仍然保持参数 old 所记录的值,满足此前提下才进行交换操作。CAS的做法类似操作数据库时常见的乐观锁机制
交换
此类操作的前缀为 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)
相对于CAS,明显此类操作更为暴力直接,并不管变量的旧值是否被改变,直接赋予新值然后返回背替换的值。
存储
此类操作的前缀为 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)
此类操作确保了写变量的原子性,避免其他操作读到了修改变量过程中的脏数据。
边栏推荐
- 最热门海量的阿里云盘资源分享
- Project Management-软件开发之项目管理
- Implementation principle and application practice of Flink CDC mongodb connector
- uv_ Run process
- Force buckle 295 Median data flow
- Force buckle 239 Sliding window Max
- Operating instructions for tcp202 current probe of Tektronix oscilloscope
- 基于Pytorch的图像分类总结:Swin Transformer
- [8. One dimensional prefix and]
- Unity3d C # generates non repeated random numbers in the interval
猜你喜欢

uv_loop_init()流程

背光模组的基本结构与应用

Project Management-软件开发之项目管理

Opencv installation (x86/tx2 cuda/ shared library)

【论文】低光图像增强的零参考深度曲线估计

【leetcode周赛总结】LeetCode第298场周赛总结(6.19)

File upload vulnerability shooting range analysis upload_ LABS

【 thesis 】 zero reference depth curve estimation for low light image enhancement

selenium入门级项目 - 豆豆玩竞猜

高考后网上查询信息,注意防范没有 SSL证书的网站
随机推荐
【爬虫笔记1】环境搭建和必要工具Selenium
Sword finger offer 56 Delete duplicate nodes of the linked list
Force buckle 239 Sliding window Max
2022年买理财产品买三个月还是半年?
MySQL field type and corresponding length & bytes
Irregular layout of table label
ZCMU--1052: Holedox Eating(C语言)
【NVMe2.0b 6】NVMe 队列模型
Jz59. print binary tree in zigzag order
ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
为什么在高并发下很容易就被setInterval给坑了
Typora + picgo configure the drawing bed to realize automatic uploading of pictures
Use the serialize common command
Lectures explanation for unsupervised graph level representation learning (usib)
Force buckle 160 Intersecting linked list
【NVMe2.0b 5】NVM Subsystem
克隆的vmware虚拟主机网卡无法启动解决办法
On the auto increment of int type primary key in MySQL
linux下安装mysql8及使用(转载)
不规范的命名