标签:port list tar get 释放 lock reading == lease
线程锁
import time import threading def addNum(): global num #在每个线程中都获取这个全局变量 print(‘get num:‘,num ) time.sleep(1) Lock.acquire() #修改数据前加锁 num +=1 #对此公共变量进行-1操作 Lock.release() #修改后释放 if __name__=="__main__": num = 100 #设定变量 thread_list = [] Lock = threading.Lock() #生成全局锁 for i in range(100): t = threading.Thread(target=addNum) t.start() thread_list.append(t) #t.join()#次加相当并行 for t in thread_list: #等待所有线程执行完毕 t.join() print(‘final num:‘, num )
标签:port list tar get 释放 lock reading == lease
原文地址:https://www.cnblogs.com/leo0362/p/10916501.html