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" ...
随机推荐
- 【linux】在ubuntu中使用apt-get安装oracle jdk6
在Ubuntu 12.04 LTS上安装JDK6本身并不复杂,只是目前较新版本的Ubuntu已经不支持直接通过apt-get安装了.因此,需要从Oracle官方网站下载安装包进行安装. 从Oracle ...
- xamarin for android 环境配置
先安装vs2010,参考以下教程可以进行破解 http://hi.baidu.com/hegel_su/item/2b0771c6aaa439e496445252?qq-pf-to=pcqq.grou ...
- wcf 基本配置
<system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...
- RPA流程自动化-UIPath简介
UiPath简介 转自: http://www.cnblogs.com/mxue/p/UiPath_To147_Road.html 最近RPA比较火,UiPath工具排名前几位并且免费试用,很多朋友们 ...
- c# winfrom 页面的enter变为tab 功能使用 在特定的按钮里面如何继续当enter使用求大神帮忙解答一下 !!急
enter 当tab 键用 已经实现 :例如按回车的时候切换一直走 ,走到一个按钮如何让回车键在这个按钮的时候还是执行enter按钮的功能而不是tab 求大神解答一下, 目前页面tab功能改为 ...
- XAMl使用其他命名空间中的类型及加载和编译
以前我们讲过XAMl命名空间.为了使便宜钱知道XAMl文档中元素对应的.NET类型,需要知道XAMl明档中指定特定的两个命名空间.XAML是一种实例化.NET对象的通用方法 ,除了可以实例化一些标准的 ...
- Node.js学习笔记(二) --- CommonJs和Nodejs 中自定义模块
一. 什么是 CommonJs? JavaScript 是一个强大面向对象语言,它有很多快速高效的解释器. 然而, JavaScript标准定义的 API 是为了构建基于浏览器的应用程序.并没有制定一 ...
- [android] 切换界面的问题
1. 界面重复创建的问题 2. 中间容器每次切换,都会清空容器中的子对象问题 3. 点击返回键的处理 解决重复创建的问题: 传递Class字节码对象,利用泛型来规定对象 判断界面是否存在,如果存在重复 ...
- C#在不同平台下DLL的引用问题
缘起 很多时候,我们需要引用在不同平台下的DLL,32位(X86)和64位(X64).如果平台错误,在C#中会引发BadImageFormatException异常. 解决思路 我们同时不能添加不同平 ...
- 流畅的python和cookbook学习笔记(一)
1.数据结构 1.1 内置序列类型 四种序列类型: 1.容器序列:list.tuple和collections.deque 2.扁平序列:str.bytes.bytearray.memoryview和 ...