也是锁,这个锁多加了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)的更多相关文章

  1. 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)

    8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...

  2. python多线程编程5: 条件变量同步-乾颐堂

    互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还 ...

  3. Python学习---线程锁/信号量/条件变量同步/线程池1221

    线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...

  4. c++并发编程之条件变量(Condition Variable)

    条件变量(Condition Variable)的一般用法是:线程 A 等待某个条件并挂起,直到线程 B 设置了这个条件,并通知条件变量,然后线程 A 被唤醒.经典的「生产者-消费者」问题就可以用条件 ...

  5. 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue

    以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...

  6. PYTHON线程知识再研习E---条件变量同步Condition

    Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...

  7. Linux 多线程条件变量同步

    条件变量是线程同步的另一种方式,实际上,条件变量是信号量的底层实现,这也就意味着,使用条件变量可以拥有更大的自由度,同时也就需要更加小心的进行同步操作.条件变量使用的条件本身是需要使用互斥量进行保护的 ...

  8. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

  9. go条件变量同步机制

    sync.Cond代表条件变量,需要配置锁才能有用 package main import ( "fmt" "runtime" "sync" ...

随机推荐

  1. EntityFrameWork Code First 多对多关系处理

    场景2: 一个文章类别(Category)下含有多篇文章(Article),而文章也可能对应多个类别 Article和Category的代码更改如下: /// <summary> /// ...

  2. WPF的布局-Grid(表格布局)

    1. Grid布局就是表格布局 如下图: 2. 使用方法 2.1. 先生成适量的行和列,代码如下: <Grid><!--使用Grid控件--> <Grid.ColumnD ...

  3. MariaDB10.2修改默认密码

    1.修改 my.ini,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2.重启 mysqld 服务 3.使用 roo ...

  4. redis虚拟内存---官方文档

    http://redis.io/topics/internals-vm Virtual Memory technical specification This document details the ...

  5. Getting over the dangers of rm command in Linux---reference

    reference:http://www.coolcoder.in/2014/01/getting-over-dangers-of-rm-command-in.html When we want to ...

  6. 使用idea开发工具,nginx服务部署Extjs6,SpringBoot项目到服务器

    编译ExtJs文件 1.输入命令 2.开始编译 3.找到编译后的文件 E:\idea_project\BaiSheng_Model\fin-ui\build\production\Admin 4.将文 ...

  7. 基于resteasy,Base64码上传文件

    package com.xgt.controller.bs; import com.xgt.bean.bs.VersionBean; import com.xgt.common.BaseControl ...

  8. HDU 5701 ——中位数计数——————【思维题】

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. MySQL 字段全部转换成小写

    原因: 因为框架某些字段大写有时候不被正确识别,所以字段都修改成小写; 特别说明:因为这里只有表,没有视图,存储过程等等其它所以我可以直接这么写; 步骤: 1.导出结构语句 2. 执行C# 脚本,替换 ...

  10. Eigen库矩阵运算使用方法

    Eigen库矩阵运算使用方法 Eigen这个类库,存的东西好多的,来看一下主要的几个头文件吧: ——Core 有关矩阵和数组的类,有基本的线性代数(包含 三角形 和 自伴乘积 相关),还有相应对数组的 ...