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" ...
随机推荐
- 关于javascript中时间格式和时间戳的转换
当前时间获取的各种函数: var myDate = new Date();myDate.getYear(); //获取当前年份(2位),已经不推荐使用myDate.getFullYear ...
- web_02Java ee实现验证码,网站访问次数功能
Web Web_02版本: 实现功能 1,验证码 2,网站访问次数统计 设计内容 1,servlet 2,jsp 3,js *重点 1,验证码相关: 1,Servlrt类实现验证码的生成 CheckC ...
- FW:Software Testing
Software Testing Testing with a Purpose Software testing is performed to verify that the completed s ...
- wrqer
- Java - 如何进行安全发布
首先让我简单解释一下所谓"发布". 发布(publish),使对象可以在当前作用域之外的代码中可见,如果该对象被发布,则该对象的非私有域中引用的所有实例同样也会被发布. 不仅仅是作 ...
- Centos7 redis 5.0 服务设置、启动、停止、开机启动
redis 没有配置服务,没有开启动,每次都要手工配置. 解决这个麻烦,我们new一个服务,然后开机启动即可. 1.创建服务(redis.conf 配置文件要注意,经过cp产生了很多个redis.co ...
- 在C++中实现类似Java的“synchronized”
我只是代码的搬运工,原文参见A "synchronized" statement for C++ like in Java.其实现是通过区域锁(Scoped locking)和宏定 ...
- mysql load data infile auto increment id
1. 问题描述 当使用load data infile 向表中插入数据 而主键id是 auto_increment 时 ,执行 load data 不会报错 但插入也不成功 2. 问题解决 2.1 方 ...
- 日常捕获的野生知识 - javascript 与 类
javascript 并不提供类这样方便实用的面向对象类型,今天学习到了,直接上代码: <!DOCTYPE html> <html lang="en"> & ...
- 29_Future模式2_JDK内置实现
[Future使用场景] Future表示一个可能未完成的一部任务的结果,针对这个结果可以添加CallBack,以便在任务执行成功或失败后作出相应的操作. Future模式非常适合在处理耗时很长的业务 ...