- lock = threading.Lock()
- 上锁lock.acquire()
- 释放锁lock.release
import threading
import time
num = 0
lock = threading.Lock()
def first():
lock.acquire()
for item in range(100000):
global num
num = num + 1
pass
lock.release()
print("rask1:", num)
pass
def second():
lock.acquire()
for item in range(100000):
global num
num = num + 1
pass
lock.release()
print("rask2:", num)
pass
if __name__ == '__main__':
first_thread = threading.Thread(target=first)
second_thread = threading.Thread(target=second)
first_thread.start()
second_thread.start()