设计基于锁的并发数据结构

1. 线程安全的栈容器

#include <exception>
#include <memory>
#include <mutex>
#include <stack>

struct empty_stack : std::exception {
	const char *what () const throw();
};

template <typename T>
class threadsafe_stack {
private:
	std::stack<T> m_data;
	mutable std::mutex m_mutex;

public:
	threadsafe_stack()
	{
	}

	threadsafe_stack (const threadsafe_stack &other)
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		m_data = other.m_data;
	}

	threadsafe_stack &operator= (const threadsafe_stack &) = delete;

	void push (T new_value)
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		m_data.push (std::move (new_value));
	}

	std::shared_ptr<T> pop ()
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		if (m_data.empty())
			throw empty_stack();
		const std::shared_ptr<T> res (std::make_shared<T> (std::move (m_data.top())));
		m_data.pop();
		return res;
	}

	void pop (T &value)
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		if (m_data.empty())
			throw empty_stack();
		value = std::move (m_data.top());
		m_data.pop();
	}

	bool empty () const
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		return m_data.empty();
	}
};

2. 线程安全的队列容器

采用条件变量实现。

#include <condition_variable>
#include <mutex>
#include <queue>
#include <thread>

template <typename T>
class threadsafe_queue {
private:
	mutable std::mutex m_mutex;
	std::queue<T> m_data_queue;
	std::condition_variable m_data_cond;
public:
	threadsafe_queue()
	{
	}

	void push (T new_value)
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		m_data_queue.push (std::move (new_value));
		m_data_cond.notify_one();
	}

	void wait_and_pop (T &value)
	{
		std::unique_lock<std::mutex> lock (m_mutex);
		m_data_cond.wait (lock, [this] { return !m_data_queue.empty(); });
		value = std::move (m_data_queue.front());
		m_data_queue.pop();
	}

	std::shared_ptr<T> wait_and_pop ()
	{
		std::unique_lock<std::mutex> lock (m_mutex);
		m_data_cond.wait (lock, [this] { return !m_data_queue.empty(); });
		std::shared_ptr<T> res (std::make_shared<T> (std::move (m_data_queue.front())));
		m_data_queue.pop();
		return res;
	}

	bool try_pop (T &value)
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		if (m_data_queue.empty())
			return false;
		value = std::move (m_data_queue.front());
		m_data_queue.pop();
		return false;
	}

	std::shared_ptr<T> try_pop ()
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		if (m_data_queue.empty())
			return std::make_shared<T>();
		std::shared_ptr<T> res (std::make_shared<T> (std::move (m_data_queue.front())));
		m_data_queue.pop();
		return res;
	}

	bool empty () const
	{
		std::lock_guard<std::mutex> lock (m_mutex);
		return m_data_queue.empty();
	}
};

参考书籍:《C++并发编程实战 第2版》

相关推荐

  1. 设计基于并发数据结构

    2024-04-12 02:08:03       19 阅读
  2. 数据库设计

    2024-04-12 02:08:03       35 阅读
  3. 设计并发分布式架构实用指南

    2024-04-12 02:08:03       37 阅读
  4. 并发

    2024-04-12 02:08:03       21 阅读
  5. 基于SpringCloudAlibaba并发流量系统设计

    2024-04-12 02:08:03       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-12 02:08:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-12 02:08:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-12 02:08:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-12 02:08:03       18 阅读

热门阅读

  1. python——循环语句

    2024-04-12 02:08:03       14 阅读
  2. 蓝桥杯备考随手记: 动态规划

    2024-04-12 02:08:03       16 阅读
  3. 生成式伪造语音安全问题与解决方案(上)

    2024-04-12 02:08:03       15 阅读
  4. redis缓存实现分布式锁原理及注意事项(附代码)

    2024-04-12 02:08:03       16 阅读
  5. 《牛客》-E可口蛋糕

    2024-04-12 02:08:03       18 阅读
  6. 原型设计模式

    2024-04-12 02:08:03       17 阅读
  7. 算法| ss 合并区间

    2024-04-12 02:08:03       15 阅读
  8. 蓝桥杯——分糖果

    2024-04-12 02:08:03       15 阅读