博客
关于我
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 order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql skip-grant-tables_MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql sysbench测试安装及命令
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>