039条件变量同步(Condition)
也是锁,这个锁多加了wait(),notify()唤醒一个进程,notifyall()唤醒全部进程方法,创建的时候默认是Rlock类型的锁,可以设置为lock类型的,默认就ok
from random import randint
import threading
import time class Producer(threading.Thread):
def run(self):
global L
while True:
val = randint(0,100)
print('生产者',self.name,':append',str(val),L)
if lock_con.acquire():
L.append(val)
lock_con.notify()
lock_con.release()
time.sleep(3) class Consumer(threading.Thread):
def run(self):
global L
while True:
lock_con.acquire()
if len(L) == 0:
lock_con.wait()
print('消费者',self.name,'delete',str(L[0]),L)
del L[0]
lock_con.release()
time.sleep(0.5) if __name__ == '__main__':
L = []
lock_con = threading.Condition()
threads = []
for i in range(5):
threads.append(Producer())
threads.append(Consumer())
for t in threads:
t.start()
for t in threads:
t.join()
使用例子
###########java改编:单生产单消费
import time
import threading class Res:
def __init__(self):
self.flag = False
self.count = 0
self.product = '' def set(self,name):
lock_con.acquire()
if self.flag:
lock_con.wait()
time.sleep(0.00001)
self.count += 1
self.product = ''.join([name,'**',str(self.count)])
self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())])
print(self.message)
self.flag = True
lock_con.notify()
lock_con.release() def get_product(self):
lock_con.acquire()
time.sleep(0.00001)
if not self.flag:
lock_con.wait()
self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())])
print(self.message)
self.flag = False
lock_con.notify()
lock_con.release() class Producer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.set('大白兔奶糖') class Consumer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.get_product() if __name__ == '__main__':
lock_con = threading.Condition()
r = Res()
c = Consumer(r)
p = Producer(r)
c.start()
p.start()
单生产单消费
############多生产多消费
import time
import threading class Res:
def __init__(self):
self.flag = False
self.count = 0
self.product = '' def set(self,name):
lock_con.acquire()
while self.flag:
lock_con.wait()
time.sleep(0.00001)
self.count += 1
self.product = ''.join([name,'**',str(self.count)])
self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())])
print(self.message)
self.flag = True
lock_con.notifyAll()
lock_con.release() def get_product(self):
lock_con.acquire()
time.sleep(0.00001)
while not self.flag:
lock_con.wait()
self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())])
print(self.message)
self.flag = False
lock_con.notifyAll()
lock_con.release() class Producer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.set('大白兔奶糖') class Consumer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.get_product() if __name__ == '__main__':
lock_con = threading.Condition()
r = Res()
l = []
for i in range(5):
l.append(Consumer(r))
for i in range(5):
l.append(Producer(r))
for a in l:
a.start()
多生产多消费
个人觉得例子理解是最好的,所以我学的东西一般使用例子
039条件变量同步(Condition)的更多相关文章
- 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)
8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...
- python多线程编程5: 条件变量同步-乾颐堂
互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还 ...
- Python学习---线程锁/信号量/条件变量同步/线程池1221
线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...
- c++并发编程之条件变量(Condition Variable)
条件变量(Condition Variable)的一般用法是:线程 A 等待某个条件并挂起,直到线程 B 设置了这个条件,并通知条件变量,然后线程 A 被唤醒.经典的「生产者-消费者」问题就可以用条件 ...
- 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue
以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...
- PYTHON线程知识再研习E---条件变量同步Condition
Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...
- Linux 多线程条件变量同步
条件变量是线程同步的另一种方式,实际上,条件变量是信号量的底层实现,这也就意味着,使用条件变量可以拥有更大的自由度,同时也就需要更加小心的进行同步操作.条件变量使用的条件本身是需要使用互斥量进行保护的 ...
- c++11多线程记录6:条件变量(condition variables)
https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...
- go条件变量同步机制
sync.Cond代表条件变量,需要配置锁才能有用 package main import ( "fmt" "runtime" "sync" ...
随机推荐
- leetcode_787【K 站中转内最便宜的航班】
有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...
- [转]asp.net5中使用NLog进行日志记录
本文转自:http://www.cnblogs.com/sguozeng/articles/4861303.html asp.net5中使用NLog进行日志记录 asp.net5中提供了性能强大的日志 ...
- MVVMLight - Messenger 2
本篇介绍MvvmLight中一个重要的东东,那就是Messenger. (一)Messenger的基本组成 Messenger类用于应用程序的通信,接受者只能接受注册的消息类型,另外目标类型可以被指定 ...
- SpringMVC国际化配置
一.什么是国际化: 国际化是设计软件应用的过程中应用被使用与不同语言和地区 国际化通常采用多属性文件的方式解决,每个属性文件保存一种语言的文字信息, 不同语言的用户看到的是不同的内容 二.spr ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter.
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc ...
- java类与对象基础--抽象类和接口
在java的类体系中,有两个比较特殊的抽象体--抽象类和接口.抽象体并不可以拥有具体的对象(而且接口甚至不是类),但是它们却是在java设计领域非常重要的两个概念,很多优秀的设计模式都是基于这两个概念 ...
- linux 安装php扩展swoole redis
本文讲的是已经有redis.so 和swoole.so文件的情况 我的环境是xampp php的扩展目录为 /opt/lampp/lib/php/extensions/no-debug-non-zts ...
- JBPM学习第3篇:10分钟熟悉JBPM工作台
1.打开:http://localhost:8080/jbpm-console 键入用户名和密码(krisv/krisv)登陆. 2.看视频: http://download.jboss.org/jb ...
- mysql三表联合查询
-- SELECT d.userId, d.userPhoNum, a.orderId, a.productType, b.courseId, b.courseName, c.payJe -- FRO ...
- Strapi 安装易错位置
Strapi官网(https://strapi.io)介绍:最先进的开源内容管理框架,可以毫不费力地构建功能强大的API,建立在Node.js平台之上,为您的API提供高速惊人的表现. 简单点说,(对 ...