线程之互斥锁

from threading import Thread, Lock
import time
n = 100
def task():
global n
mutex.acquire() #互斥锁枷锁
temp = n
time.sleep(0.0001)
n = temp - 1
mutex.release() #互斥锁解锁
if __name__ == '__main__':
mutex = Lock()
t_l = list()
for i in range(100):
t = Thread(target=task)
t_l.append(t)
t.start()
for t in t_l:
t.join()
print('主', n)

你可能感兴趣的:(线程之互斥锁)