首页 > 编程语言 > 详细

python多线程同步机制condition

时间:2017-09-24 12:31:24      阅读:291      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import time



def customer(cond):
t = threading.currentThread()
with cond:
# wait()方法创建了一个名为waiter的锁,并且设置锁的状态为locked。这个waiter锁用于线程间的通讯
cond.wait()
print ‘{}: Resource is available to consumer‘.format(t.name)

def producer(cond):
t = threading.currentThread()
with cond:
print ‘{}: Making resource available‘.format(t.name)
cond.notifyAll()


if __name__ == "__main__":
cond = threading.Condition()
c1 = threading.Thread(target=customer, args=(cond,), name=‘c1‘)
c2 = threading.Thread(target=customer, args=(cond,), name=‘c2‘)
p1 = threading.Thread(target=producer, args=(cond,), name=‘p1‘)

c1.start()
c2.start()
p1.start()


print ‘Main end‘

python多线程同步机制condition

原文:http://www.cnblogs.com/zejin2008/p/7586683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!