基于锁的线程安全队列实现

#pragma once
#include<memory>
#include<mutex>
#include<condition_variable>
template<typename T>
class threadsafe_queue
{
	using namespace std;
private:
	struct node
	{
		std::shared_ptr<T> data;
		std::unique_ptr<node> next;
	};
	std::mutex mx_head;
	std::mutex mx_tail;
	std::unique_ptr<node> head;
	node* tail;
	std::condition_variable data_cond;
public:
	//空队列是头尾指针都指向一个默认的节点
	threadsafe_queue() :head(new node), tail(head.get()) {}
	threadsafe_queue(const threadsafe_queue&) = delete;
	threadsafe_queue& operator =(const threadsafe_queue&) = delete;

	/*外部用这些函数就可以*/
	//出队列并获取出队的对头值
	std::shared_ptr<T> try_pop();
	bool try_pop(T& value);

	std::shared_ptr<T> wait_and_pop();
	void wait_and_pop(T& value);
	void push(T NewValue);
	void empty();

	//这里没有提供size 因为是并发数据结构 若要了解队列中元素个数 可以自己定义一个原子变量 push成功加1 pop成功减1

	/*下面都是函数是内部使用的*/
private:
	node*  get_tail()
	{
		std::lock_guard<std::mutex> lk(mx_tail);
		return tail;
	}
	std::unique_ptr<node> pop_head()
	{
		std::unique_ptr<node> old_head = std::move(head);
		head = std::move(old_head->next);
		return old_head;
	}
	std::unique_ptr<node> try_pop_head()
	{
		std::lock_guard<mutex>headlock(mx_head);
		if (head.get() == get_tail())
			return unique_ptr<node>();
		return pop_head();
	}
	std::unique_ptr<node> try_pop_head(T&value)
	{
		std::lock_guard<mutex>headlock(mx_head);
		if (head.get() == get_tail())
			return unique_ptr<node>();
		value = std::move(*head->data);
		return pop_head();
	}
	std::unique_lock<std::mutex> wait_for_data()
	{
		std::unique_lock<std::mutex> headlock(mx_head);
		data_cond.wait(&headlock, [this]{ return head.get() != get_tail(); });
		return std::move(headlock);
	}
	std::unique_ptr<node> wait_pop_head()
	{
		std::unique_lock<std::mutex> headlock(wait_for_data());
		return pop_head();
	}
	std::unique_ptr<node> wait_pop_head(T&value)
	{
		std::unique_lock<std::mutex> headlock(wait_for_data());
		value = std::move(*head->data);
		return pop_head();
	}

};

template<typename T>
inline std::shared_ptr<T> threadsafe_queue<T>::try_pop()
{
	 std::unique_ptr<node>old_head=try_pop_head();
	 return old_head ? old_head->data : std::shared_ptr<T>();
}

template<typename T>
inline bool threadsafe_queue<T>::try_pop(T& value)
{
	std::unique_ptr<node>old_head = try_pop_head(value);
	return old_head;
}

template<typename T>
inline std::shared_ptr<T> threadsafe_queue<T>::wait_and_pop()
{
	const std::unique_ptr<node> old_head = wait_pop_head();
	return old_head->data;

}

template<typename T>
inline void threadsafe_queue<T>::wait_and_pop(T& value)
{
	const std::unique_ptr<node> old_head = wait_pop_head(value);
}

template<typename T>
inline void threadsafe_queue<T>::push(T NewValue)
{
	std::shared_ptr<T> data(std::make_shared(std::move(NewValue)));
	std::unique_ptr<node> pNode = std::make_unique();

	{
		std::lock_guard<std::mutex> lk(mx_tail);
		Node*const  Newtail = pNode.get();
		tail->data = data;
		tail->next = std::move(pNode);
		tail = Newtail;
	}
	data_cond.notify_one();
}

template<typename T>
inline void threadsafe_queue<T>::empty()
{
	std::lock_guard<std::mutex>headlock(mx_head);
	return (head.get() == get_tail());
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值