其他分享
首页 > 其他分享> > 线程锁

线程锁

作者:互联网

数据安全?
线程锁 GLI 全局解释器锁
l = threading.Lock()
l.acquire() # 加锁
l.release() # 释放锁
demo:

import threading

a=1
l= threading.Lock()
def adda(i):
l.acquire() # 线程锁
print("这是线程",i)
global a
a+=1
print("a等于",a)
l.release() # 释放线程锁


for i in range(5):
t = threading.Thread(target=adda, args=[i, ]) # 创建线程对象
t.start() # 启用线程

标签:Lock,acquire,threading,线程,adda,print
来源: https://www.cnblogs.com/zhangmeiyan/p/14750651.html