当前位置:网站首页>[thread knowledge points] - spin lock
[thread knowledge points] - spin lock
2022-07-25 14:53:00 【Senior fishing Engineer】
List of articles
Preface
Spin lock is a basic knowledge point , Should be familiar with the master .
One 、 What is spin lock ?
The spin ” It can be understood as “ Self rotation ”, there “ rotate ” finger “ loop ”, such as while Loop or for loop .“ The spin ” Is that I keep cycling here , Until the goal is achieved . Not like ordinary locks , If you can't get the lock, you go into blocking .
The spin lock does not release CPU Time slice of , Instead, try to acquire the lock by spinning , If the attempt fails , Then keep trying , Until it is successfully obtained .
Two 、 Why use a spin lock ?
If the content of a critical zone is very “ Short ”, Then, if the lock cannot be obtained by using threads “ Blocking ” To lock , Then the cost of thread state switching is likely to be greater than the cost of executing the content of the critical area , Poor efficiency . If a thread has been found in the critical area “ Occupy ” Under the circumstances , Through a certain number of [ The spin ] To wait for the thread that has obtained the permission to use the critical area to release the lock , Then you can avoid frequent thread switching to a certain extent , To improve efficiency .
3、 ... and 、 Instance to explain
1.5 Start ,java.util.concurrent.atomic The atomic classes in the package basically have the shadow of spin lock .
AtomicInteger
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}
Take a look at this self incrementally invoked method
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
there do while It's a spin operation , If you encounter other threads competing for modification during the modification process, resulting in your own modification failure , Just try to add one again in a dead cycle , Until we succeed .
Four 、 Implement a reentrant spin lock by yourself
TODO : ==''
5、 ... and 、 Disadvantages and adaptation scenarios
shortcoming
While avoiding the overhead caused by thread state switching , Introduced new overhead , Because spinlocks need to keep trying to get locks . If the lock has not been released , Or the lock takes too long , Will lead to useless attempts to spin , Wasted processor resources .
Applicable scenario
The use of spin lock needs to meet the following two conditions at the same time :
- The degree of concurrency is not high
- The critical zone is short and uncomplicated
The above two conditions are actually to ensure that the spin can end as soon as possible , Avoid waste CPU resources .
summary
Spin lock can improve program efficiency , But we must pay attention to the use scenario , Otherwise it would be counterproductive .
边栏推荐
- 河源市区推出消防安全主题奶茶 助推夏季火灾防控
- 006 operator introduction
- Leetcode-198- house raiding
- Sudo rosdep init error ROS installation problem solution
- Gonzalez Digital Image Processing Chapter 1 Introduction
- QObject source code analysis -d pointer and Q pointer
- 43 box model
- sudo rosdep init Error ROS安装问题解决方案
- Quickly set up dobbo demo
- Melody + realsense d435i configuration and error resolution
猜你喜欢

37 element mode (inline element, block element, inline block element)

Resource not found: rgbd_launch 解决方案

Sudo rosdep init error ROS installation problem solution

云安全技术发展综述

关于ROS2安装connext RMW的进度条卡在13%问题的解决办法

43 盒子模型

【口才】谈判说服技巧及策略

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

51 single chip microcomputer learning notes (2)

Vs2017 large factory ERP management system source code factory general ERP source code
随机推荐
006 operator introduction
软件测试 -- 1 软件测试知识大纲梳理
微信公众号正式环境上线部署,第三方公众平台接入
[C topic] Li Kou 206. reverse the linked list
[C题目]力扣876. 链表的中间结点
Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
51单片机学习笔记(2)
gson与fastjson
Examples of bio, NiO, AIO
39 简洁版小米侧边栏练习
Melodic + Realsense D435i 配置及错误问题解决
[Nuxt 3] (十一) 传送 & 模块
I2C device driver hierarchy
IP address classification, which determines whether a network segment is a subnet supernetwork
Vs2017 large factory ERP management system source code factory general ERP source code
关于RDBMS和非RDBMS【数据库系统】
Gameframework making games (II) making UI interface
Log4j2 basic configuration
(original) customize a scrolling recyclerview
Ssh server rejected password