博客
关于我
c++11并发编程历程(8)stack类中固有的竞争和解决方法(互斥锁的保护范围,即颗粒度)
阅读量:728 次
发布时间:2019-03-21

本文共 3862 字,大约阅读时间需要 12 分钟。

固有竞争来源

通过实现,我们知道了可以通过互斥元保护共享数据。但仅仅通过互斥元或其他机制保护共享数据,并不足够防止竞争条件,因而仍需确保保护了适当的数据。以双向链表的例子说明,在安全地删除链表结点时,必须管理对结点指针的并发访问(涉及要删除的结点及其两边结点)。单独保护每个结点的指针并不能提升安全性,因为竞争条件仍可能发生。

考虑到std::stack适配器的接口,empty()size()可能在被调用时正确,但在调用之后使用之前其他线程可能执行push()pop()操作,这就没有保障。

以下是对std::stack示例代码的简要说明:

template 
>class stack {public: stack() : stack(Container()) {} explicit stack(const Container&); explicit stack(Container&); template
explicit stack(const Alloc&); template
stack(const Container&, const Alloc&); template
stack(Container&, const Alloc&); template
stack(const stack&, const Alloc&); template
stack(stack&, const Alloc&); [[nodiscard]] bool empty() const { return c.empty(); } size_type size() const { return c.size(); } reference top() { return c.back(); } const_reference top() const { return c.back(); } void push(const value_type& x) { c.push_back(x); } void push(value_type&& x) { c.push_back(std::move(x)); } template
decltype(auto) emplace(Args&... args) { return c.emplace_back(std::forward
(args...)); } void pop() { c.pop_back(); } void swap(stack& s) noexcept(is_nothrow_swappable_v
) { using std::swap; swap(c, s.c); }};

需要注意的是,仅仅因为某些操作是线程安全的,并不意味着整个接口是线程安全的。即便是非常简单的接口,如果有共享实例,也可能面临线程安全问题。

竞争的常用解决方法

通过传入引用来避免异常拷贝

一种常见的方法是将希望接收出栈值的变量通过引用传递给pop()函数。例如:

std::vector
result;some_stack.pop(result);

这种方式的好处是后台如何变化不会影响当前线程。然而,它的不足在于调用pop()之前必须首先构造一个目标对象,这在某些情况下会带来很高的时间和资源成本。此外,目标类型必须支持赋值,例如需要有拷贝构造函数或移动构造函数。对于许多用户定义的类型来说,这不是总是可行的。

弃用传入引用改用安全的拷贝或移动

为了确保线程安全,可以使用不引发异常的拷贝构造函数和移动构造函数。根据C++标准,std::is_nothrow_copy_constructiblestd::is_nothrow_move_constructible可以用于检测是否存在安全的拷贝或移动构造函数。

最终,这种方法仍面临限制。许多用户定义类型不能保证拷贝构造函数不会引发异常。

返回指向出栈顶的指针

另一种解决方案是返回一个指向出栈顶的指针。这种方式避免了拷贝构造函数潜在的异常问题。对于简单类型(如整数),这种方式没有内存管理的额外开销。但是对于复杂对象,这种方式需要使用std::shared_ptr来管理内存,可能会带来额外的性能开销。

综合使用多种方法

在实际应用中,通常需要结合多种方法。例如,传入引用适用于拷贝构造函数不会引发异常的情况,而返回指针则用于需要高效内存管理的复杂对象。

实现线程安全的栈

线程安全栈的基本类定义

#include 
#include
#include
#include
struct empty_stack : std::exception { const char *what() const throw();};template
class threadsafe_stack {private: std::stack
data; std::mutex m;public: threadsafe_stack(); threadsafe_stack(const threadsafe_stack& other) { std::lock_guard
lock(other.m); data = other.data; } void push(t new_value) { std::lock_guard
lock(m); data.push(new_value); } std::shared_ptr
pop() { std::lock_guard
lock(m); if (data.empty()) { throw empty_stack(); } auto res(std::make_shared
(data.top())); data.pop(); return res; } void pop(t& value) { std::lock_guard
lock(m); if (data.empty()) { throw empty_stack(); } value = data.top(); data.pop(); } bool empty() const { std::lock_guard
lock(m); return data.empty(); }};

通过这一实现,我们确保了所有的操作都在互斥保护下完成。这个设计不仅避免了竞争条件,还简化了接口,减少了潜在的错误点。

线程安全栈的详细实现

#include 
#include
#include
#include
struct empty_stack : std::exception { const char *what() const throw();};template
class threadsafe_stack {private: std::stack
data; std::mutex m;public: threadsafe_stack() {} threadsafe_stack(const threadsafe_stack& other) { std::lock_guard
lock(other.m); data = other.data; } void push(t new_value) { std::lock_guard
lock(m); data.push(new_value); } std::shared_ptr
pop() { std::lock_guard
lock(m); if (data.empty()) { throw empty_stack(); } auto res(std::make_shared
(data.top())); data.pop(); return res; } void pop(t& value) { std::lock_guard
lock(m); if (data.empty()) { throw empty_stack(); } value = data.top(); data.pop(); } bool empty() const { std::lock_guard
lock(m); return data.empty(); }};

此实现的关键点在于确保所有操作都在互斥锁下进行。这样可以保证即使在高并发环境下,也能正确地管理栈数据,避免竞争条件带来的数据不一致问题。同时,通过返回std::shared_ptr,我们可以有效地管理内存分配,减少newdelete操作的开销。

转载地址:http://wfngz.baihongyu.com/

你可能感兴趣的文章
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>
mysql5.6.21重置数据库的root密码
查看>>
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>