当前位置:网站首页>spinlock 使用介绍
spinlock 使用介绍
2022-07-20 21:10:00 【顺其自然~】
一、spinlock 简介
自旋锁(spinlock):是指当一个线程在获取锁的时候,如果锁已经被其它线程获取,那么该线程将循环等待,不断尝试获取锁,直到获取到锁才会退出循环。
二、自旋锁与互斥锁的区别
自旋锁与互斥锁类似,它们都是为了解决对某项资源的互斥使用,在任何时刻最多只能有一个线程获得锁。
对于互斥锁,如果资源已经被占用,调用者将进入睡眠状态
对于自旋锁,如果资源已经被占用,调用者就一直循环在那里,看是否自旋锁的保持者已经释放了锁
三、自旋锁的优缺点
自旋锁不会使线程状态发生切换,不会使线程进入阻塞状态,减少了不必要的上下文切换,执行速度快。非自旋锁在获取不到锁的时候会进入阻塞状态,从而进入内核态,当获取到锁的时候需要从内核态恢复,需要线程上下文切换,影响性能。
如果某个线程持有锁的时间过长,就会导致其它等待获取锁的线程长时间循环等待消耗CPU,造成CPU使用率极高。
原文链接:https://blog.csdn.net/chenwh_cn/article/details/117227454
边栏推荐
猜你喜欢
Section 13 of Chapter 2: intersection of set And Cross set
Nacos cluster construction
asp. Net - edit hyperlink event
Section 10 of Chapter 2: supplementary knowledge points of the list
第二章第十一节:元组
DAMA-第六章(数据存储与操作)
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
解决npm 安装 nrm遇到报错问题SyntaxError: Unexpected token import
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
第二章第十节:列表的补充知识点
随机推荐
DAMA-第六章(数据存储与操作)
第二章第三节:字符串常规操作
Section 8 of Chapter II: addition, deletion, modification and query of the list
[daily question] 1260 2D mesh migration
Section 3 of Chapter 2: general operation of string
Is the reverse repurchase of treasury bonds safe? How to sell it
修改电脑注册表,将VSCode添加至鼠标右键菜单
Deep copy function
基于可见光通信的移动机器人室内定位及物联网应用
微信小程序实现网易云音乐唱片机播放效果
Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
C call stored procedure to transfer table valued parameters
Section 7 of Chapter 2: the concept of list
leetcode:42. Connect rainwater
Intel assembly language programming learning - Chapter 5 process - Part 1
Devaxpress xtrareport report development related knowledge points
Multiplier technology cloud management and control platform adapts to Alibaba cloud polardb, and jointly promotes the prosperity of cloud native database ecosystem
PLC的通信模式
面试中常见的的 web 安全问题
Internship: several steps to get familiar with the project code