C++自定应线程安全数据结构(1)

线程安全的栈

该线程安全栈的作用是,允许多个线程对栈进行操作,不必再栈上进行加锁,而是栈本身内部封装了锁的机制。操作的本身不是并行化的,因为不可能同时对栈既添加数据,又取出数据;其真正的意义是多个线程访问时,避免上述不安全的情况发生。

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

// 自定义异常
struct empty_stack: std::exception {
    const char* what() const throw() {};
};

template<typename T>
class threadsafe_stack {
  public:
    threadsafe_stack() {}
    threadsafe_stack(const threadsafe_stack& other) {
        std::lock_guard<std::mutex> lock(m);
        data = other.data;
    }
    
    // 不允许直接复制
    threadsafe_stack& operator=(const threadsafe_stack&) = delete;
    
    // 元素入栈操作 
    void push(T new_value) {
        std::lock_guard<std::mutex> lock(m);
        data.push(std::move(new_value));
    }
    // 元素出栈,返回指针
    std::shared_ptr<T> pop() {
        std::lock_guard<std::mutex> lock(m);
        if(data.empty()) {
            throw empty_stack();
        }
        std::shared_ptr<T>const res(std::make_shared<T>(std::move(data.top())));
        data.pop();
        return res;
    }
    // 元素出栈,传递引用的方式
    void pop(T &value) {
        std::lock_guard<std::mutex> lock(m);
        if(data.empty()) {
            throw empty_stack();
        }
        value = std::move(data.top());
        data.pop();
    }
    // 判断栈是否是空的
    bool empty()const {
        std::lock_guard<std::mutex> lock(m);
        return data.empty();
    }
  private:
    std::stack<T>data;
    mutable std::mutex m;
};

补充说明:

  • mutable关键字:该关键字与const相对应,在const函数中,原则上不允许修改成员变量,但是如果声明为mutable,那么可以进行修改。
  • what():该函数是继承标准异常是需要重写的函数,参考这篇博客

线程安全的队列

普通版本的线程安全队列:

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

template<typename T>
class threadsafe_queue {
public:
	threadsafe_queue() {}

	threadsafe_queue(const threadsafe_queue& other) {
		std::lock_guard<std::mutex> lock(mtx);
		data_queue = other.data_queue;
	}

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

	void push(T new_value) {
		std::lock_guard<std::mutex> lock(mtx);
		data_queue.push(std::move(new_value));
		data_cond.notify_one();
	}

	void wait_and_pop(T& value) {
		std::unique_lock<std::mutex>lock(mtx);
		data_cond.wait(lock, [this] {return !data_queue.empty();});
		data_queue.pop();
	}

	std::shared_ptr<T>wait_and_pop() {
		std::unique_lock<std::mutex>lock(mtx);
		data_cond.wait(lock, [this] {return !data_queue.empty();});
		data_queue.pop();
	}

	bool try_pop(T& value) {
		std::lock_guard<std::mutex>lock(mtx);
		if (data_queue.empty()) {
			return std::shared_ptr<T>();
		}
		std::shared_ptr<T>res(std::make_shared<T>(std::move(data_queue.front())));
		data_queue.pop();
		return res;
	}

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

private:
	mutable std::mutex mtx;
	std::queue<T> data_queue;
	std::condition_variable data_cond;
};
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值