python同步锁
加上同步锁后,锁住的线程代码必须执行完后,才允许另一个线程开启.下面的代码显示了同步锁的应用
import time,threading def subNum(): global num r.acquire() temp = num num = temp - 1 r.release() if __name__ == '__main__': num = 100 r = threading.Lock() for i in range(100): t = threading.Thread(target = subNum) t.start() print('the final number is:%s'%num)