当前位置:网站首页>Important knowledge of golang: atomic atomic operation
Important knowledge of golang: atomic atomic operation
2022-06-23 15:24:00 【yue_ xin_ tech】
sync/atomic Introduce
When we want to make concurrent and safe changes to a variable , In addition to using official mutex, You can also use sync/atomic The atomic operation of the package , It can ensure that the reading or modification of variables is not affected by other processes .
atomic Bag Atomic manipulation It's through CPU Instructions , That is, it is implemented at the hardware level , Good performance , No need to be like mutex Record a lot of states like that . Of course ,mutex Not just concurrency control of variables , More is the concurrency control of code blocks ,2 The focus is different .
sync/atomic operation
atomic There are several atomic operations , Mainly Add、CompareAndSwap、Load、Store、Swap.
Add
atomic Of Add Is aimed at int and uint To add atomic value :
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 comparison and exchange method implements a function similar to optimistic lock , Only the original value and the passed in old Have the same value , Will modify :
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 It is possible to produce ABA Phenomenon occurs . That is, the original value is A, Later modified B, Later, it is modified to A. In this case, it also conforms to CompareAndSwap The rules , Even if it has been changed halfway .
Load
Load The method is to prevent during reading , Other processes initiate modification actions , Affect the reading results , Commonly used in Configuration item The entire read .
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
There are atoms reading , There are atomic modification values , As mentioned earlier Add Only applicable to int、uint Increase or decrease of type , There are no other types of modifications , and Sotre Methods by unsafe.Pointer Pointer atom modification , To achieve 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
Swap Method realizes the atomic exchange of pairs of values , Not only int,uint You can exchange , The pointer can also .
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)
summary
atomic Many times it may not be used , After all mutex The expansibility of is quite good , It is also friendly to use . But this does not hinder our pursuit of ultimate performance , occasionally , Details determine performance !
Interested friends can search the official account 「 Read new technology 」, Pay attention to more push articles .
If you can , Just like it by the way 、 Leave a message 、 Under the share , Thank you for your support !
Read new technology , Read more new knowledge .
边栏推荐
- Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
- 【云驻共创】智能供应链计划:提升供应链决策水平,帮助企业运营降本增效
- JS中的pop()元素
- Origin of sectigo (Comodo) Certificate
- Horizon development board commissioning
- 2021-06-07
- 2021-05-22
- Gartner's latest report: development of low code application development platform in China
- 2021-04-15
- Golang -- multiple processing scenarios for files
猜你喜欢
The idea and method of MySQL master-slave only synchronizing some libraries or tables

The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine

基因检测,如何帮助患者对抗疾病?

Half wave loss equal thickness and equal inclination interference

从3开始,在业务系统中增加分页功能

SQL注入漏洞(原理篇)

30. concatenate substrings of all words

Gartner's latest report: development of low code application development platform in China

labelme的JSON文件转成COCO数据集格式

golang 重要知识:waitgroup 解析
随机推荐
JS traversal array (using the foreach () method)
MySQL advanced statement 2
Solution to the problem that MySQL cannot be started in xampp
When I went to oppo for an interview, I got numb...
JS garbage collection
2021-06-03
WebService interface publishing and calling
Why is Xiaomi stuck in the chip quagmire?
AXI_ Round_ Robin_ Arbiter design - aw and W channels
Mysql数据库---日志管理、备份与恢复
[普通物理] 光的衍射
Mysql database - log management, backup and recovery
Golang -- multiple processing scenarios for files
php 二维数组插入
SQL injection vulnerability (principle)
从3开始,在业务系统中增加分页功能
PHP specified fields are more than 100 in positive order and less than 100 in random order
【opencv450】椒盐噪声demo
MySQL高级语句一
JS create an array (literal)