13.锁的应用

import threading

g_num = 0
def fun():
    global g_num
    for i in range(10000000):
        mutexFlag = mutex.acquire(True)
        if mutexFlag:
            g_num += 1
            mutex.release()
    print("num:%d"%g_num)

if __name__ == "__main__":
    mutex = threading.Lock()
    t1 = threading.Thread(target=fun)
    t2 = threading.Thread(target=fun)

    t1.start()
    t2.start()

 

posted @ 2018-03-22 23:01  Bob__Zhang  阅读(120)  评论(0编辑  收藏  举报