《C++ Concurrency in Action》笔记7 mutex(3)pop和top问题之示例
来源:互联网 发布:三维模具设计软件 编辑:程序博客网 时间:2024/05/17 23:28
采用上一篇所说的方案一和方案三定义的一个线程安全的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
- 《C++ Concurrency in Action》笔记7 mutex(3)pop和top问题之示例
- 《C++ Concurrency in Action》笔记7 mutex(3)pop和top问题之方案
- 《C++ Concurrency in Action》笔记7 mutex(2)pop和top问题
- 《C++ Concurrency in Action》笔记7 mutex(1)
- 《C++ Concurrency in Action》笔记1 join和detach
- 《C++ Concurrency in Action》笔记3 move线程对象
- C plus plus Concurrency in Action
- 读C++ concurrency in action笔记
- 《C++ concurrency in action》第二章笔记
- 《C++ Concurrency in Action》笔记 前言
- 《C++ Concurrency in Action》笔记4 hardware_concurrency()
- 《C++ Concurrency in Action》笔记14 condition_variable
- 《C++ Concurrency in Action》笔记16 future
- 《C++ Concurrency in Action》笔记17 promise
- 《C++ Concurrency in Action》笔记26 原子操作和强制排序
- 《C++ Concurrency in Action》笔记2 线程函数传参(3)类成员函数
- 《C++ Concurrency in Action》笔记4 vector<thread>
- 《C++ Concurrency in Action》笔记5 std::thread::id
- 数据结构顺序表的基本算法
- Online Judge 错误提示
- #leetcode编程日记#547. Friend Circles
- 小小打灰机——1-制作游戏首界面和滚动背景
- 如何写SysV服务管理脚本
- 《C++ Concurrency in Action》笔记7 mutex(3)pop和top问题之示例
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B题 coin
- SSH框架搭建
- ubuntu16.04 ROS环境下配置和运行SVO
- ubuntu16.04主题美化和软件推荐
- 老师分配办公室的应用练习
- CRF模型在NLP中的运用
- 正则表达式和表单验证
- 杂记1--虚析构函数