栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

#pragma once
#include
#include
#include
template
class threadsafe_queue
{
	using namespace std;
private:
	struct node
	{
		std::shared_ptr data;
		std::unique_ptr next;
	};
	std::mutex mx_head;
	std::mutex mx_tail;
	std::unique_ptr 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 try_pop();
	bool try_pop(T& value);

	std::shared_ptr 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 lk(mx_tail);
		return tail;
	}
	std::unique_ptr pop_head()
	{
		std::unique_ptr old_head = std::move(head);
		head = std::move(old_head->next);
		return old_head;
	}
	std::unique_ptr try_pop_head()
	{
		std::lock_guardheadlock(mx_head);
		if (head.get() == get_tail())
			return unique_ptr();
		return pop_head();
	}
	std::unique_ptr try_pop_head(T&value)
	{
		std::lock_guardheadlock(mx_head);
		if (head.get() == get_tail())
			return unique_ptr();
		value = std::move(*head->data);
		return pop_head();
	}
	std::unique_lock wait_for_data()
	{
		std::unique_lock headlock(mx_head);
		data_cond.wait(&headlock, [this]{ return head.get() != get_tail(); });
		return std::move(headlock);
	}
	std::unique_ptr wait_pop_head()
	{
		std::unique_lock headlock(wait_for_data());
		return pop_head();
	}
	std::unique_ptr wait_pop_head(T&value)
	{
		std::unique_lock headlock(wait_for_data());
		value = std::move(*head->data);
		return pop_head();
	}

};

template
inline std::shared_ptr threadsafe_queue::try_pop()
{
	 std::unique_ptrold_head=try_pop_head();
	 return old_head ? old_head->data : std::shared_ptr();
}

template
inline bool threadsafe_queue::try_pop(T& value)
{
	std::unique_ptrold_head = try_pop_head(value);
	return old_head;
}

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

}

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

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

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

template
inline void threadsafe_queue::empty()
{
	std::lock_guardheadlock(mx_head);
	return (head.get() == get_tail());
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号