博客
关于我
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 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>