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()