线程同步互斥的方法

线程的event:

  创建对象:
    e = threading.Event()

  事件阻塞函数
    e.wait([timeout])

  设置事件
    e.set()

  清除事件
    e.clear()

线程锁:

lock = threading.Lock() 创建锁对象
lock.acquire() 上锁
lock.release() 解锁
*上锁状态条用acquire会阻塞
with lock 上锁

import threading

a = b =0
lock = threading.Lock()    #创建锁

def value():
    while True:
        with lock:    #上锁
            if a != b:
                print("a = %d,b = %d"%(a,b))

t = threading.Thread(target = value)
t.start()

while True:
    with lock:
        a += 1
        b += 1

t.join
原文地址:https://www.cnblogs.com/zengsf/p/9649418.html