Linux C++ 使用condition实现阻塞队列的方法
作者:jingxian 发布时间:2023-11-12 10:09:45
标签:阻塞队列,linux
实例如下:
/*
* BlockingQueue.h
*
* Created on: 2014年6月10日
* Author:
*/
#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_
#include <iostream>
#include <pthread.h>
using namespace std;
//template <typename T >
class BlockingQueue
{
public:
BlockingQueue();
BlockingQueue(int capacity);
~BlockingQueue();
bool push(int item);
int poll();
private:
int capacity;
int* queue;
int head,tail;
pthread_mutex_t mutex;
pthread_cond_t notFull,notEmpty;
};
#endif /* BLOCKINGQUEUE_H_ */
/*
* BlockingQueue.cpp
*
* Created on: 2014年6月10日
* Author:
*/
#include "../include/BlockingQueue.h"
BlockingQueue::BlockingQueue()
{
this->capacity = 10;
queue = new int[capacity];
head = 0,tail = 0;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(¬Full,NULL);
pthread_cond_init(¬Empty,NULL);
}
BlockingQueue::BlockingQueue(int capacity)
{
this->capacity = capacity;
queue = new int[capacity];
cout << "capacity " << sizeof(queue) << endl;
head = 0,tail = 0;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(¬Full,NULL);
pthread_cond_init(¬Empty,NULL);
}
BlockingQueue::~BlockingQueue()
{
this->capacity = 0;
head = 0,tail = 0;
delete queue;
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(¬Full);
pthread_cond_destroy(¬Empty);
}
bool BlockingQueue::push(int item)
{
pthread_mutex_lock(&mutex);
cout << "you want push " << item << endl;
while((head + 1) % capacity == tail)//is full
{
cout << "is full,wait..." << endl;
// push wait
pthread_cond_wait(¬Full,&mutex);
cout << "not full,unlock" << endl;
}
{
queue[head] = item;
head = (head + 1) % capacity;
cout << "push " << item << endl;
//wake up poll thread
pthread_cond_signal(¬Empty);
pthread_mutex_unlock(&mutex);
return true;
}
}
int BlockingQueue::poll()
{
pthread_mutex_lock(&mutex);
int ret = 0;
while(head == tail) // is empty
{
cout << "is empty,wait..." << endl;
//poll wait
pthread_cond_wait(¬Empty,&mutex);
cout << "not empty,unlock..." << endl;
}
{
ret = queue[tail];
tail = (tail + 1) % capacity;
cout << "take " << ret << endl;
//wake up push thread
pthread_cond_signal(¬Full);
pthread_mutex_unlock(&mutex);
return ret;
}
}
#include <iostream>
#include "include/BlockingQueue.h"
using namespace std;
BlockingQueue queue(3);
void* put(void *)
{
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
return NULL;
}
void* take(void *)
{
queue.poll();
queue.poll();
queue.poll();
return NULL;
}
int main() {
pthread_t put1,take1;
pthread_create(&put1,NULL,put,0);
pthread_create(&take1,NULL,take,0);
void * retval;
pthread_join(put1,&retval);
pthread_join(take1,&retval);
return 0;
}
0
投稿
猜你喜欢
- Windows2003服务器安装及设置教程——注册表篇 防DDOS洪水攻击处理reg add "HKEY_CURRENT
- www的日志一般都在%winsystem%\system32\logfiles\w3svc1下面,包括www日志和ftp日志,一般先停止ww
- Discuz!是个不错的玩意, X2已经算是很棒了,但毕竟还有很多不足的地方,比如X2调用活动主题时,会在内容输出的地方自动添加活动图片、参
- 首先我们需要使用 cd 命令进入对应的目录,然后,命令如下:find . -type f -atime +365 -ex
- 今年新做了个地方网站 喀克网 www.kakew.com现在将我在宣传中的各项开支给大家报一下,或许对大家有帮助:1、设计LOGO 费用10
- 北京时间10月16日消息,据国外媒体报道,互联网安全软件厂商AVG周四发布报告称,包括MyGirlySpace、Ferraritone、Ma
- godaddy是全球最大的域名注册商,这家公司是美国公司,最近他们进入了中国市场,并且支持了支付宝付款这种方式,现在购买godaddy域名已
- 百度老总李彦宏出了一道题:“评价一个搜索引擎好坏的标准到底是什么?”这位刚成为武大名誉教授的李老师,承诺将回答优秀者收作自己的研究生。我对这
- 安装篇2003默认安装不带IIS的,要安装,请点击开始->管理工具->配置您的服务器向导然后一步步的下一步。到了列表选择项目的时
- 今天我们公司开始上班了,又投入到了紧张的工作状态中,接下来的每天都是密切的关注这搜索引擎的动静以及公司那些网站的最近情况。其实说实话,企业网
- 一切本来都是那样的宁静,所有的网络服务都在默默地工作着。然而近一段时间来,经常有人打电话反映一个相同的问题:在接收E-Mail时,服务器端经
- 现在调查公司均针对作弊行为加强了许多防范措施,其防范主表现在调查过程当中的所设置的种种陷阱,有些陷井不为人所知,甚至把一些符合调查要求的人员
- 随着Web数据库的应用越来越广泛,Web数据库的安全问题日益突出,如何才能保证和加强数据库的安全性已成为目前必须要解决的问题。数据库系统安全
- 简介yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件
- 我这里有好几个tomcat,同时使用的话就要给端口号改成不一样的:1、首先cd进tomcat目录里的conf文件夹,vi打开server.x
- 首先要提到的是这里的内网是指一般我们网吧内部环境的内网,对局域网内网我没什么好说的,愿花钱的去DNS0755好了。一个基本条件:一个公网IP
- Linux 中firewall的使用方法总结 firewall 简单使用 1,显示所有配置firewall-cmd --
- Quickstart Guide The Foreman installer is a collection of Puppet modul
- 老谢老是听刚开始学软文的学生抱怨写软文没有灵感,其实灵感也是因为你的思想,软文灵感不是天生的,它可以学习和创造!“软文”,顾名思义,就在于个
- 使用ping命令,通过返回的TTL值可以判断主机的操作系统。如果TTL值为128就可以认为目标主机的操作系统是Windows NT/2000