当前位置:网站首页>Notes on concurrent programming of vegetables (V) thread safety and lock solution
Notes on concurrent programming of vegetables (V) thread safety and lock solution
2022-04-23 10:34:00 【Ape knowledge】

Series index : Concurrent programming notes of vegetables | Python Concurrent programming details ( Continuous updating ~)
One 、 Introduction to thread safety concepts

Two 、Lock Used to solve thread safety problems

The first way is to manually obtain , Manually release... After execution , It cannot be locked without releasing other programs . The second is easier to use ,with It will release resources after program execution .
3、 ... and 、 Code details
We use threading Module built in Lock Method to create a lock object , Take withdrawing money as an example , The following code can only be executed after obtaining the lock .
stay main In the function, we initialize a Account object , Create two threads ta and tb, Let both threads start executing , The first thread executes normally , The second thread can only obtain the lock after the first thread is executed . If this code is unlocked , If two threads execute at the same time, the balance will be negative .
import threading
import time
lock = threading.Lock()
class Account:
def __init__(self, balance):
self.balance = balance
def draw(account, amount):
with lock:
if account.balance >= amount:
time.sleep(0.1)
print(threading.current_thread().name," Take money successfully ")
account.balance -= amount
print(threading.current_thread().name," balance ", account.balance)
else:
print(threading.current_thread().name," Failed to withdraw money , Lack of balance ")
if __name__ == "__main__":
account = Account(1000)
ta = threading.Thread(name="ta", target=draw, args=(account, 800))
tb = threading.Thread(name="tb", target=draw, args=(account, 800))
ta.start()
tb.start()
utilize Lock To solve thread safety problems is very common , In the next article, let's use Thread pool To solve this kind of problem , Semaphore mechanism It also plays a big role in thread safety , Welcome to your attention , I will keep updating ~
Python Advanced concurrent programming is continuously updated , welcome
Like collection+Focus on
Last one : Concurrent programming notes of vegetables |( Four )Python Actual producer - Consumer mode multithreaded crawler
Next : Concurrent programming notes of vegetables |( 6、 ... and ) How to use thread pool to transform crawler program
My level is limited , Please comment and correct the deficiencies in the article in the comment area below ~If feelings help you , Point a praise Give me a hand ~
Share... From time to time Interesting 、 Have a material 、 Nutritious content , welcome Subscribe to follow My blog , Looking forward to meeting you here ~
版权声明
本文为[Ape knowledge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230619310059.html
边栏推荐
- LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
- 二叉树的构建和遍历
- Zhengda international explains what the Dow Jones industrial index is?
- Sim Api User Guide(7)
- C language - custom type
- What are the system events of Jerry's [chapter]
- DBA common SQL statements (1) - overview information
- Leetcode22: bracket generation
- 定义链表(链表)
- CSP certification 202203-2 travel plan (multiple solutions)
猜你喜欢

Zhengda international explains what the Dow Jones industrial index is?

Solution architect's small bag - 5 types of architecture diagrams

【leetcode】107.二叉树的层序遍历II

Question bank and answers of Shanghai safety officer C certificate examination in 2022

Charles 功能介绍和使用教程

Sim Api User Guide(4)

SQL Server 递归查询上下级

/Can etc / shadow be cracked?

Reading integrity monitoring techniques for vision navigation systems - 3 background

MapReduce compression
随机推荐
【leetcode】199.二叉树的右视图
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
Image processing - Noise notes
Using multithreading to output abc10 times in sequence
Construction and traversal of binary tree
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
Solution architect's small bag - 5 types of architecture diagrams
JVM——》常用命令
/Can etc / shadow be cracked?
Sim Api User Guide(7)
第120章 SQL函数 ROUND
Question bank and answers of Shanghai safety officer C certificate examination in 2022
Chapter II in memory architecture (im-2.2)
IDEA——》每次启动都会Indexing或 scanning files to index
Leetcode22:括号生成
域名和IP地址的联系
24. Exchange the nodes in the linked list (linked list)
Ansible cloud computing automation
Chapter 1 Oracle database in memory related concepts (im-1.1)
JUC concurrent programming 09 -- source code analysis of condition implementation