《C++ Concurrency in Action》笔记7 mutex(3)pop和top问题之示例

采用上一篇所说的方案一和方案三定义的一个线程安全的stack,它其实是个stack的包装类,代码如下:

struct empty_stack : exception
{
	const char* what() const throw()
	{
		return "the stack is empty";
	};
};
template<typename T>
class threadsafe_stack
{
private:
	stack<T> data;
	mutable mutex m;
public:
	threadsafe_stack() {}
	threadsafe_stack(const threadsafe_stack& other)
	{
		lock_guard<mutex> lock(other.m);
		data = other.data;
	}
	threadsafe_stack& operator=(const threadsafe_stack&) = delete;
	void push(T new_value)
	{
		lock_guard<mutex> lock(m);
		data.push(new_value);
	}
	shared_ptr<T> pop()
	{
		lock_guard<mutex> lock(m);
		if (data.empty()) 
			throw empty_stack();
		shared_ptr<T> const res(make_shared<T>(data.top()));
		data.pop();
		return res;
	}
	void pop(T& value)
	{
		lock_guard<mutex> lock(m);
		if (data.empty()) 
			throw empty_stack();
		value = data.top();
		data.pop();
	}
	bool empty() const
	{
		lock_guard<mutex> lock(m);
		return data.empty();
	}
};
为了最大化其安全性,整个stack的操作都被严格限制。赋值操作被删除,但是可以拷贝构造,假设其元素类型支持拷贝。2个pop()函数有可能抛出empty_stack异常,保证即使stack为空的情况下执行pop也没有问题。接口由原来的5个变为现在的3个:push()、pop()、empty(),尽管empty()有些多余。

注意他的拷贝构造函数,他没有在初始化列表中初始化成员,而是在函数体内赋值。这样做的目的是为了让mutex起到保护作用。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值