上面写错了函数名 是mutex.acquire()
import threading
from threading import Thread
from threading import Lock
number=0
lock=Lock()
def work3():
global number
for i in range(1000000):
lock.acquire()#添加锁
number+=1
lock.release()#释放锁
print(number)
def work4():
global number
for i in range(1000000):
lock.acquire(blocking=True)#添加锁
number+=1
lock.release()#释放锁
print(number)
if __name__ == '__main__':
t3=Thread(target=work3)
t4=Thread(target=work4)
t3.start()
t4.start()
t3.join()
t4.join()#如果这里不加join 输出的number 可能不是最终结果
print(number)