1 import threading 2 import time 3 globals_num = 0 4 lock = threading.RLock() 5 def func(): 6 lock.acquire()#获得锁 7 global globals_num 8 globals_num += 1 9 time.sleep(1) 10 print(globals_num) 11 lock.release()#释放锁 12 13 for i in range(10): 14 t = threading.Thread(target=func) 15 t.start()