Python锁概念

创建锁

mutex = threading.Look()

等待事件

mutex.acquire(blockTime)
不加等待时间就会一直等待

释放锁

mutex.release()

from threading import Thread
from threading import Look


thnum = 0
class MyTread(Thread):
    def run(self):
        mutex.acquire()
        for i in range(1000)
            global thnum
            thnum +=1
        print(thnum)
        mutex.release()


def test():
    global thnum
    mutex.acquire()
    for i in range(1000):
        thnum += 1
    print(thnum)
    mutex.release()     # 解锁

mutex = Look()

if __name__ == '__main__':
    t = MyThread()
    t.start()

#  创建一把互斥锁

thn = Thread(target=test)
thn.start()    

你可能感兴趣的:(Python文档)