C++无锁队列实现 (2012-05-15 07:46:26)
2014-07-04 18:01
330 查看
转载▼
标签:
| 分类: 常用源码 |
#ifndef LOCK_FREE_QUEUE_H_
#define LOCK_FREE_QUEUE_H_
//不加锁队列,适合一个线程读取,一个线程写
#include <list>
template <typename T>
class LockFreeQueue
{
public:
LockFreeQueue()
{
list.push_back(T());//分割节点
iHead = list.begin();
iTail = list.end();
};
void Produce(const T&
t) //存消息
{
list.push_back(t);
iTail = list.end();
list.erase(list.begin(), iHead);
};
bool Consume(T&
t) //取消息
{
typename TList::iterator
iNext = iHead;
++iNext;
if (iNext
!= iTail)
{
iHead = iNext;
t = *iHead;
return true;
}
return false;
};
bool Peek(T&
t) //查看消息不删除
{
typename TList::iterator
iNext = iHead;
++iNext;
if (iNext
!= iTail)
{
t = *iNext;
return true;
}
return false;
}
bool IsEmpty()
{
typename TList::iterator
iNext = iHead;
++iNext;
if (iNext
!= iTail)
{
return false;
}
else
{
return true;
}
}
int GetMaxSize()
{
return list.max_size();
};
private:
typedef std::list<T>
TList;
TList list;
typename TList::iterator
iHead, iTail;
};
#endif
相关文章推荐
- c++模板实现的队列
- C++ 学习练手 - 数组型队列的模板实现
- C++ 大根堆 实现优先队列
- 算法导论 第六章优先队列C++实现
- 算法导论 第六章优先队列C++实现
- 循环队列的实现(Queue, C++版)
- 简单循环队列(C++模版技术实现)
- 循环队列的定义、入队、出队等操作 C++代码实现
- c++实现的循环队列和栈
- 杨辉三角之c++队列实现
- 程序员面试题精选(48):两个堆栈模拟队列c++代码实现
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 链队列的C++ 实现以及循环队列
- 链队列的实现,C++代码实现
- c++实现队列操作(建对、入队、出对)
- 最小堆优先队列C++实现
- C++队列实现和队列简介(不是循环队列)
- 【C++】队列——顺序表实现V1.0(源码提供)
- 优先级队列——用C++模板实现
- 队列 C++实现