当前位置:网站首页>Understanding of deadlock
Understanding of deadlock
2022-07-30 10:19:00 【@Luna】
Deadlock problem with bank transfers
I. Overview
1. Bank transfer problem (A to B transfers 1,000 yuan, involving the cash balance of two users)
The transfer business is a kind of transaction, so it is necessary to ensure the security of the data.Note is an atomic operation.
(1) If the balance of user A is less than the balance to be transferred, the balance of the transferor cannot be deducted.
(2) If the balance of user A is less than the balance to be transferred, the balance of the transferor cannot be deducted, and the transfer is stopped to remind the transferor that the transfer failed.
(3) If there is an error in the middle of the transfer (account A is successfully transferred out and deducted, account B fails to be added/A transfers to B and B transfers to A, the order of acquiring the locks is reversed, resulting in a deadlock), the data needs to be returned.roll.
2. For a transfer operation, 2 locks are required, one locks the balance of the transfer account, and the other locks the balance of the transfer account.But the sequence of locking will cause the program to deadlock.
3. The situation of deadlock: A transfers 100 to B (when the t1 thread executes the transfer from A to B, the account lock of A is successfully acquired + dormant + waiting to acquire the lock of B account), and B also transfers 100 to A (t2When the thread executes the transfer from B to A, the lock of the account B is successfully acquired + also waiting to acquire the lock of the A account), resulting in a deadlock
Second, the four elements of deadlock
1. Mutual exclusion, shared resources X and Y can only be occupied by one thread;
2. Occupy and wait, thread T1 has acquired shared resource X, and does not release shared resource X while waiting for shared resource Y;
3. No preemption, other threads cannot forcibly preempt the resources occupied by thread T1;
4. Circular waiting, thread T1 waits for the resources occupied by thread T2, and thread T2 waits for the resources occupied by thread T1, which is circular waiting;
III. Bank transfer code:
Deadlock version code:



Avoid deadlock version:
Original rule:
Get the sender first, and then acquire the lock of the payee, which will cause two senders to wait for the other party to release the lock.
Changed rules:
All transactions acquire smaller locks first, and then acquire larger locks before acquiring smaller locks, which avoids a circular deadlock.The size of the two locks is the same, and an additional lock is required to control the transaction process.
The principle I understand:
1. Because the two objects are transferred at the same time, the two objects are equals, so the one with the smaller hashcode value is fixed, and the value is locked first each timeSmall, it can ensure that each thread is locked sequentially to avoid deadlock.
2. Even if the two hashcodes are equal, you can first compete for the third lock to ensure that one person completes the business and releases the lock first, so that there will be no locks that each owns a lock and waits for the object to be released at the beginning.situation.




边栏推荐
- 学习笔记10--局部轨迹生成主要方法
- 时刻铭记:总有一天你将破蛹而出
- CVTE school recruitment written test questions + summary of knowledge points
- 105. 从前序与中序遍历序列构造二叉树(视频讲解!!)
- SST-Calib: A lidar-visual extrinsic parameter calibration method combining semantics and VO for spatiotemporal synchronization calibration (ITSC 2022)
- Day113.尚医通:微信登录二维码、登录回调接口
- Re20:读论文的先例:普通法的信息理论分析
- MFCC转音频,效果不要太逗>V<!
- 编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
- 【 HMS core 】 【 】 the FAQ HMS Toolkit collection of typical questions 1
猜你喜欢

Security思想项目总结

shell脚本

(C语言)文件操作

A new generation of free open source terminal tool, so cool

Domino Server SSL Certificate Installation Guide
容器技术 -- 简单了解 Kubernetes 的对象

ESP32 入门篇(一)使用 VS Code 进行开发环境安装

Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court

STM32CubeMX configuration to generate FreeRTOS project

SST-Calib: A lidar-visual extrinsic parameter calibration method combining semantics and VO for spatiotemporal synchronization calibration (ITSC 2022)
随机推荐
PyQt5-在窗口上绘制文本
软考 系统架构设计师 简明教程 | 案例分析 | 需求分析
what is this method called
Study Notes 10--Main Methods of Local Trajectory Generation
PyQt5 - draw sine curve with pixels
PyQt5 - Drawing different types of straight lines
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
快解析结合任我行crm
最长公共序列、串问题总结
STM32CubeMX configuration to generate FreeRTOS project
CVTE校招笔试题+知识点总结
元宇宙改变人类工作模式的四种方式
Redis Desktop Manager 2022.4.2 发布
flowable workflow all business concepts
4、yolov5-6.0 ERROR: AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘ 解决方案
实战演练 | 在 MySQL 中计算每日平均日期或时间间隔
HR团队如何提升效率?人力资源RPA给你答案
(BUG record) No module named PIL
大数据产品:标签体系0-1搭建实践
多线程--线程和线程池的用法