c++自定义队列
来源:互联网 发布:c语言实验交换字母 编辑:程序博客网 时间:2024/05/01 02:07
#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_
#include <list>
#include <vector>
#include <queue>
#include <utils/Mutex.h>
#include <semaphore.h>
#include <utils/Condition.h>
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&)
namespace android {
template<typename T, int count>
class BlockingQueue {
private:
mutable std::list<T> mList;
mutable Mutex mLock;
mutable Condition mContentAvailableCondition;
//volatile int m_size;
T front(bool remove) {
Mutex::Autolock autolock(mLock);
while ( mList.empty() ) {
int ret =0;
ret = mContentAvailableCondition.waitRelative(mLock, 200000000);
//m_size = mList.size();
// ALOGD("wujw %s %d %lld %d %d %d %d \n", __FUNCTION__, __LINE__, m_size, &m_size, ret, mList.empty(), gettid());
}
T e = *(mList.begin());
if (remove) {
mList.erase(mList.begin());
}
return e;
}
DISALLOW_COPY_AND_ASSIGN(BlockingQueue);
public:
BlockingQueue() {
}
~BlockingQueue() {
}
bool empty() {
Mutex::Autolock autolock(mLock);
return mList.empty();
}
int size() {
Mutex::Autolock autolock(mLock);
return mList.size();
}
void clear() {
Mutex::Autolock autolock(mLock);
mList.clear();
}
T peek() {
return front(false);
}
T pop() {
//ALOGD("wujw %s %d \n", __FUNCTION__, __LINE__);
return front(true);
}
void push(T e) {
Mutex::Autolock autolock(mLock);
if(mList.size( ) <= count){
mList.push_back(e);
//ALOGD("wujw %s %d %d \n", __FUNCTION__, __LINE__,mList.size( ));
}
//m_size = mList.size( );
//ALOGD("wujw %s %d %d %lld %d \n", __FUNCTION__, __LINE__,m_size,&m_size,gettid());
mContentAvailableCondition.signal();
}
};
} /* namespace android */
#endif
typedef struct{
char buf[100];
int nLen;
}TestData;
class Test{
BlockingQueue<TestData, 20> testQueue;
};
readDataThread()
{
TestData testdata;
ret = read(fd,testdata.buf,len);
testdata.nLen = ret;
pThis->testQueue.push(testdata);
}
sendDataThread()
{
TestData testdata;
pThis->testQueue.pop(testdata);
ret = write(fd,testdata.buf,testdata.nLen);
}
0 0
- c实现自定义消息队列实例源码
- 自定义队列
- 自定义队列
- C 队列
- 队列(C#)
- C 队列
- C+队列
- C 队列
- java自定义队列
- 自定义字节缓冲队列
- 用单链表自定义队列
- 优先队列 自定义优先级
- 自定义队列MyArray
- 链表自定义队列
- c++自定义队列
- 自定义消息队列
- 自定义优先队列
- 自定义栈和队列
- 写给新人看的自定义View-onMeasure篇(2)
- lintcode397最长上升连续子序列(动态规划easy)
- 自定义指令2
- 颜色列表(中英文名称,RGB HSV CMYK值)
- [Object
- c++自定义队列
- 设置欢迎界面设置
- 轻松自动化---selenium-webdriver(python) (十二)--转自虫师
- Java代码编译和执行的整个过程
- SpringMVC 使用@ResponseBody返回json 中文乱码
- docker distritubion源码分析之二:storage driver详解(基于2.6.0版本)
- 前端培训班出身——吐槽下16年的艰辛
- 信号傅立叶变换后实际频谱图(Matlab)
- Javascript 中的神器——Promise