线程标准库模块--threading(之线程互斥机制--线程锁Lock)
线程锁
1、创建线程对象
lock = threading.Lock()
2、上锁
lock.acquire()
3、解锁
lock.release()
with lock 上锁
import threading
a = b = 0
lock = threading.Lock()
def value():
while True:
lock.acquire()
if a != b:
print("a = %d,b = %d"%(a,b))
lock.release()
t = threading.Thread(target = value)
t.start()
while True:
lock.acquire()
a += 1
b += 1
lock.release()
t.join()