当前位置:网站首页>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)
此类操作确保了写变量的原子性,避免其他操作读到了修改变量过程中的脏数据。
边栏推荐
- web框架概述与程序开发
- Lectures explanation for unsupervised graph level representation learning (usib)
- 【NVMe2.0b 6】NVMe 队列模型
- unity3D C# 在区间内生成不重复的随机数
- Moorish voting
- fastdfs-5.0.5安装
- 360edr planing
- Force buckle 102 Sequence traversal of binary tree
- Unity3d C # generates non repeated random numbers in the interval
- Configuration and optimization of redis cache database
猜你喜欢

xpm_ memory_ A complete example of using the tdpram primitive

Operating instructions for tcp202 current probe of Tektronix oscilloscope

Library management system (PHP final report)

uv_ Run process

【NVMe2.0b 6】NVMe 队列模型

关于mysql的int型主键自增问题

【Percona-Toolkit】系列之pt-table-checksum和pt-table-sync 数据校验修复神器

你是一名技术管理者还是项目管理者?
![[pwn basics]pwntools learning](/img/67/6fb8a9628d5b750b0396f083aaeb91.png)
[pwn basics]pwntools learning

【NVMe2.0b 5】NVM Subsystem
随机推荐
fastdfs5.0.11安装
[crawler notes 2] mouse events, screenshots and common attack methods
[nvme2.0b 5] sous - système nvm
圖數據庫ONgDB Release v-1.0.2
[9. submatrix sum]
Conference chat room - development documents
ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
Day17QtQLcdNumber2021-10-22
xpm_ memory_ A complete example of using the tdpram primitive
Implementation principle and application practice of Flink CDC mongodb connector
【爬虫笔记2】鼠标事件与截图方法、常用攻击方法
[summary of leetcode weekly competition] summary of the 298th weekly competition of leetcode (6.19)
Flink CDC MongoDB Connector 的实现原理和使用实践
Primary key in efcore
【NVMe2.0b 9】控制器初始化过程
2022 low voltage electrician test question simulation test question bank and answers
C2-qt serial port debugging assistant 2021.10.21
Using open source software to save an enterprise level map data platform solution
opencv安装(x86/tx2 cuda/共享库)
【NVMe2.0b 10】Controller Shutdown 与 NVM Subsystem Shutdown