【Python】多线程的互斥锁


# mutex

from threading import Thread, Lock
import time

n = 100


def task():
global n
mutex.acquire()
temp = n
time.sleep(0.01)
n = temp - 1
mutex.release()


if __name__ == '__main__':
mutex = Lock()
t_l = []
for i in range(100):
t = Thread(target=task)
t_l.append(t)
t.start()

for t in t_l:
t.join()

print('main', n)
posted @ 2018-08-26 17:52  caya  阅读(88)  评论(0编辑  收藏  举报