• 并发编程四(4) 线程同步 Condition


    Condition

    import threading
    import time
    def consumer(cond):
        with cond:
            print("consumer before wait")
            cond.wait() # 等待消费(相当于进程就绪状态)
            print("consumer after wait")
    
    def producer(cond):
        with cond:
            print("producer before notifyAll")
            # cond.notify_all() # 通知所有等待cond的消费者可以消费了
            cond.notify()  # 唤醒一个等待cond的消费者
            print("producer after notifyAll")
    
    if __name__ == '__main__':
        condition = threading.Condition()
        t1 = threading.Thread(name = "thread-1", target = consumer, args=(condition,))
        t2 = threading.Thread(name = "thread-2", target = consumer, args=(condition,))
        t3 = threading.Thread(name = "thread-3", target = producer, args=(condition,))
    
        t1.start()
        time.sleep(2)
        t2.start()
        time.sleep(2)
        t3.start()
  • 相关阅读:
    进程虚拟内存
    非连续内存区缺页异常处理
    请求调页和写时复制
    标签对齐(补充)
    shell数学表达式
    缺页异常的处理
    不错的书籍
    imag database2
    image database
    Apache down了?
  • 原文地址:https://www.cnblogs.com/kknote/p/16103449.html
Copyright © 2020-2023  润新知