#include <pthread.h>
#include <list> using namespace std; template <typename T> class wqueue
{
list<T> m_queue;
pthread_mutex_t m_mutex;
pthread_cond_t m_condv; public:
wqueue() {
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_condv, NULL);
}
~wqueue() {
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_condv);
}
void add(T item) {
pthread_mutex_lock(&m_mutex);
m_queue.push_back(item);
pthread_cond_signal(&m_condv);
pthread_mutex_unlock(&m_mutex);
}
T remove() {
pthread_mutex_lock(&m_mutex);
while (m_queue.size() == ) {
pthread_cond_wait(&m_condv, &m_mutex);
}
T item = m_queue.front();
m_queue.pop_front();
pthread_mutex_unlock(&m_mutex);
return item;
}
int size() {
pthread_mutex_lock(&m_mutex);
int size = m_queue.size();
pthread_mutex_unlock(&m_mutex);
return size;
}
};
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "thread.h"
#include "wqueue.h" class WorkItem
{
string m_message;
int m_number; public:
WorkItem(const char* message, int number)
: m_message(message), m_number(number) {}
~WorkItem() {} const char* getMessage() { return m_message.c_str(); }
int getNumber() { return m_number; }
}; class ConsumerThread : public Thread
{
wqueue<WorkItem*>& m_queue; public:
ConsumerThread(wqueue<WorkItem*>& queue) : m_queue(queue) {} void* run() {
// Remove 1 item at a time and process it. Blocks if no items are
// available to process.
for (int i = ;; i++) {
printf("thread %lu, loop %d - waiting for item...\n",
(long unsigned int)self(), i);
WorkItem* item = m_queue.remove();
printf("thread %lu, loop %d - got one item\n",
(long unsigned int)self(), i);
printf("thread %lu, loop %d - item: message - %s, number - %d\n",
(long unsigned int)self(), i, item->getMessage(),
item->getNumber());
delete item;
}
return NULL;
}
}; int main(int argc, char** argv)
{
// Process command line arguments
if ( argc != ) {
printf("usage: %s <iterations>\n", argv[]);
exit(-);
}
int iterations = atoi(argv[]); // Create the queue and consumer (worker) threads
wqueue<WorkItem*> queue;
ConsumerThread* thread1 = new ConsumerThread(queue);
ConsumerThread* thread2 = new ConsumerThread(queue);
thread1->start();
thread2->start(); // Add items to the queue
WorkItem* item;
for (int i = ; i < iterations; i++) {
item = new WorkItem("abc", );
queue.add(item);
item = new WorkItem("def", );
queue.add(item);
item = new WorkItem("ghi", );
queue.add(item);
sleep();
} // Wait for the queue to be empty
while (queue.size() < );
printf("done\n");
exit();
}

producter-consumer 他山之石的更多相关文章

  1. Producter and Consumer

    package pinx.thread; import java.util.LinkedList; import java.util.Queue; public class ProducerConsu ...

  2. python系列之 - 并发编程(进程池,线程池,协程)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  3. Springboot+ActiveMQ(ActiveMQ消息持久化,保证JMS的可靠性,消费者幂等性)

    ActiveMQ 持久化设置: 在redis中提供了两种持久化机制:RDB和AOF 两种持久化方式,避免redis宕机以后,能数据恢复,所以持久化的功能 对高可用程序来说 很重要. 同样在Active ...

  4. python并发编程之进程池,线程池,协程

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  5. concurrent.futures模块(进程池/线程池)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  6. Linux互斥锁、条件变量和信号量

    Linux互斥锁.条件变量和信号量  来自http://kongweile.iteye.com/blog/1155490 http://www.cnblogs.com/qingxia/archive/ ...

  7. python课堂整理20----生产者消费者模型

    一.实现功能:店铺生产包子,消费者来吃 import time def producter(): ret = [] for i in range(10): time.sleep(0.1) ret.ap ...

  8. Python之网络编程之concurrent.futures模块

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  9. python并发编程之进程池、线程池、协程

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  10. Java基础——消息队列

    1.消息队列的适用场景:商品秒杀.系统解耦.日志记录等 2.使用Queue实现消息对列 双端队列(Deque)是 Queue 的子类也是 Queue 的补充类,头部和尾部都支持元素插入和获取阻塞队列指 ...

随机推荐

  1. Spring Boot学习笔记---Spring Boot 基础及使用idea搭建项目

    最近一段时间一直在学习Spring Boot,刚进的一家公司也正好有用到这个技术.虽然一直在学习,但是还没有好好的总结,今天周末先简单总结一下基础知识,等有时间再慢慢学习总结吧. Spring Boo ...

  2. 安装ipython和jupyter

    本节内容: 安装ipython 安装jupyter Pycharm介绍 Python软件包管理 一.安装ipython 1. python的交互式环境   2. 安装ipython 可以使用pip命令 ...

  3. maven环境的配置,如果jar包下载不下来,其他配置无错误的话,极有可能是网速的缘故

    1首先下载apach maven 2配置maven环境变量 m2_home  maven的源文件的路径 path变量后跟 %m2_home%\bin 3cmd 控制台运行mvn -version 查看 ...

  4. 005.HAProxy+Keepalived高可用负载均衡

    一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...

  5. Linux成长之路

    Linux命令格式: 命令   选项 参数command [-options] [parameter1] ···· 常用命令: tree 以目录树的方式显示: tree / 以目录树方式显示根目录结构 ...

  6. Java基础--对象的序列化

    所有分布式应用常常需要跨平台,跨网络,因此要求所有传的参数.返回值都必须实现序列化. 比如常见的Dubbo分布式平台,里面的对象实体类必须实现序列化才能在网络间传递 一.定义 序列化:把Java对象转 ...

  7. 事物注解方式: @Transactional

    当标于类前时, 标示类中所有方法都进行事物处理 , 例子: 1 @Transactional public class TestServiceBean implements TestService { ...

  8. MySQL中查询获取每个班级成绩前三名的学生信息

    CREATE TABLE t_testscore( pk_id INT PRIMARY KEY, c_name VARCHAR(50) , c_score INT, c_class INT )DEFA ...

  9. Shiro笔记(二)身份验证

    Shiro笔记(二)身份验证 一.核心代码 @Test public void helloWorldTest(){ IniSecurityManagerFactory factory = new In ...

  10. Android MediaPlayer架构 -- 前言小知识点(二)

    本文系作者自己学习之所用,文章内容仅出自作者拙劣之思考,问题之处烦请不吝指教. 在frameworks\av\media\libmedia\mediaplayer.cpp中会有语句:const sp& ...