05 锁 同步 互斥锁

from multiprocessing import Queue

import queue
import time
from threading import Lock,Thread
num = 100
def f1(loc):
    # num -= 1
    loc.acquire()
    global num
    # num -= 1
    tmp = num
    tmp -= 1
    time.sleep(0.001)
    num = tmp
    loc.release()
if __name__ == '__main__':
    t_loc = Lock()
    t_list = []
    for i in range(10):
        t = Thread(target=f1,args=(t_loc,))
        t.start()
        t_list.append(t)
    [tt.join() for tt in t_list]
    print('主线的num',num)

  

原文地址:https://www.cnblogs.com/work14/p/10267180.html