基于条件变量的消息队列
作者:网络转载 发布时间:[ 2013/4/23 10:09:30 ] 推荐标签:
BlockingQueue.cpp 文件代码
/*
* BlockingQueue.cpp
*
* Created on: Apr 19, 2013
* Author: archy_yu
*/
#include "BlockingQueue.h"
BlockingQueue::BlockingQueue()
{
pthread_mutex_init(&this->_mutex,NULL);
pthread_cond_init(&this->_cond,NULL);
}
BlockingQueue::~BlockingQueue()
{
pthread_mutex_destroy(&this->_mutex);
pthread_cond_destroy(&this->_cond);
}
int BlockingQueue::peek(CommonItem &item)
{
if( !this->_read_queue.empty() )
{
item = this->_read_queue.front();
this->_read_queue.pop();
}
else
{
pthread_mutex_lock(&this->_mutex);
while(this->_write_queue.empty())
{
pthread_cond_wait(&this->_cond,&this->_mutex);
}
while(!this->_write_queue.empty())
{
this->_read_queue.push(this->_write_queue.front());
this->_write_queue.pop();
}
pthread_mutex_unlock(&this->_mutex);
}
return 0;
}
int BlockingQueue::append(CommonItem item)
{
pthread_mutex_lock(&this->_mutex);
this->_write_queue.push(item);
pthread_cond_signal(&this->_cond);
pthread_mutex_unlock(&this->_mutex);
return 0;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11