线程安全队列(三)
来源:互联网 发布:工程算量软件 编辑:程序博客网 时间:2024/05/22 17:02
利用自己实现的容器结构,链表来实现更加细的锁,
从而增强并发性
/**
* @brief CFineLockQueue 利用自己实现的容器队列
* @brief 创建细粒度更高的锁,从而实现并发
*/
template<typename T>
class CFineLockQueue : public CThreadSafeQueue<T>
{
public:
CFineLockQueue() : head(new node), tail(head.get())
{}
virtual ~CFineLockQueue() {}
/**
* @brief 复制构造函数,需要加锁处理
*/
CFineLockQueue(const CFineLockQueue& other) = delete;
CFineLockQueue& operator=(const CFineLockQueue& other) = delete;
/**
* @brief 入队列加锁保护
* @param new_value 入队列的值
*/
virtual void push(T new_value);
/**
* @brief 出队列加锁保护, 返回计数指针防止泄露
*/
virtual std::shared_ptr<T> pop();
/**
* @brief 出队列加锁保护, 返回引用,返回方式与上述有所不同
* @param value 返回的值
*/
virtual void pop(T& value);
/**
* @brief 队列是否为空
*/
virtual bool empty();
private:
/**
* @brief 链表的节点
*/
struct node
{
/**
* @brief data 存储的数据
*/
T data;
/**
* @brief 链表的下一个节点指针
*/
std::unique_ptr<node> next;
};
/**
* @brief 头结点锁
*/
std::mutex head_mutex;
/**
* @brief 头结点指针
*/
std::unique_ptr<node> head;
/**
* @brief 尾结点锁
*/
std::mutex tail_mutex;
/**
* @brief 尾结点指针
*/
node* tail;
/**
* @brief 获取尾节点指针
*/
node* get_tail()
{
std::lock_guard<std::mutex> tail_lock(tail_mutex);
return tail;
}
/**
* @brief 弹出头结点
*/
std::unique_ptr<node> pop_head()
{
std::lock_guard<std::mutex> head_lock(head_mutex);
if (head.get() == get_tail())
{
return nullptr;
}
std::unique_ptr<node> old_head = std::move(head);
head = std::move(old_head->next);
return old_head;
}
};
template<typename T>
void CFineLockQueue<T>::push(T new_value)
{
std::unique_ptr<node> p(new node);
node* new_tail = p.get();
std::lock_guard<std::mutex> tail_lock(tail_mutex);
tail->data = std::move(new_value);
tail->next = std::move(p);
tail = new_tail;
}
template<typename T>
std::shared_ptr<T> CFineLockQueue<T>::pop()
{
std::unique_ptr<node> old_head = pop_head();
if (old_head)
{
return std::make_shared<T>(std::move(old_head->data));
}
else
{
return std::shared_ptr<T>();
}
}
template<typename T>
void CFineLockQueue<T>::pop(T& value)
{
std::unique_ptr<node> old_head = pop_head();
value = old_head->data;
}
template<typename T>
bool CFineLockQueue<T>::empty()
{
std::lock_guard<std::mutex> head_lock(head_mutex);
return head.get() == get_tail();
}
- 线程安全队列(三)
- 线程安全队列(一)
- 安全线程队列(二)
- 安全线程队列(四)
- JAVA线程安全队列(一)
- JAVA线程安全队列(二)
- 线程安全的队列
- linux 线程安全队列
- C++线程安全队列
- 线程安全随机队列
- 线程安全的队列
- 队列的线程安全
- 安全线程队列
- 深入浅出GUI线程安全(三)
- Servlet(三)servlet线程安全
- Python线程编程(三)同步队列
- 多写多读-线程安全队列1
- 多写多读-线程安全队列2
- SeqListD 顺序表的动态实现
- 100_容器_List_ArrayList_LinkedList_Vector用法_详解
- 用malloc函数实现一维数组的模拟
- caffe绘制训练过程的loss和accuracy曲线
- 写一个函数,判断一个字符串是否为回文联
- 线程安全队列(三)
- Tarjan算法求桥和割点
- Git基础教程
- 进程、线程与协程
- echarts3--tooltip-position
- 《大话数据结构》第四章 队列
- mysql 实现 substring+lastindexof 的方法
- 关于变量交换(异或)
- HDU 5127 Dogs' Candies(瞎暴力)