一、锁
1、多个线程操作同一个数据的时候,就的加锁
实例:
import threading num = 0 lock = threading.Lock() #申请一把锁 def add(): global num #lock.acquire() #加锁 #num+=1 #lock.release() #解锁 with lock: #简写,用with会自动帮你加锁,解锁 num = num + 1 for i in range(20): t = threading.Thread(target=add,) t.start() while threading.activeCount() !=1: pass print(num)
原文:https://www.cnblogs.com/xumb/p/11964217.html