# -*- coding:utf-8 -*- # 同步锁: 将并行装换为串行 在加锁 后只允许一个线程执行 解锁前 其他线程等待 # 注意加锁位置 import threading import time num = 100 def foo(): global num loc.acquire() # 加锁 temp = num time.sleep(0.01) num = temp -1 loc.release() # 解锁 l = [] loc = threading.Lock() # 创建锁对象 for i in range(100): t = threading.Thread(target=foo) t.start() l.append(t) for e in l: e.join() print(num) # even 对象 # 改变执行顺序 even.wait()表示等待 even.set()后运行 # 一个wait set 后 even.clear() 在继续下一个even.wait()
同步锁acquire(),release() even对象
猜你喜欢
转载自blog.csdn.net/weixin_42100915/article/details/80421525
今日推荐
周排行