C++ Concurrent in Action(英文版)书上(No.52-No.53)写的hierarchical_mutex函数,只适合结合std::lock_guard使用,直接使用如果不考虑顺序,可能会出现问题。

参看hierarchical_mutex类的代码:

#include <mutex>

class hierarchical_mutex
{
std::mutex internal_mutex;
unsigned long const hierarchy_value;
unsigned long previous_hierarchy_value;
static thread_local unsigned long this_thread_hierarchy_value;
void check_for_hierarchy_violation()
{
if (this_thread_hierarchy_value <= hierarchy_value)
{
throw std::logic_error("mutex hierarchy violated");
}
}
void update_hierarchy_value()
{
previous_hierarchy_value = this_thread_hierarchy_value;
this_thread_hierarchy_value = hierarchy_value;
}
public:
explicit hierarchical_mutex(unsigned long value) :
hierarchy_value(value),
previous_hierarchy_value()
{} void lock()
{
check_for_hierarchy_violation();
internal_mutex.lock();
update_hierarchy_value();
}
void unlock()
{
this_thread_hierarchy_value = previous_hierarchy_value;
internal_mutex.unlock();
}
bool try_lock()
{
check_for_hierarchy_violation();
if (!internal_mutex.try_lock())
return false;
update_hierarchy_value();
return true;
} static unsigned long get_thread_hierarchy_value()
{
return this_thread_hierarchy_value;
}
}; thread_local unsigned long
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX);

测试代码:

#include <iostream>
#include "hierarchical_mutex.h" hierarchical_mutex m1();
hierarchical_mutex m2();
hierarchical_mutex m3();
hierarchical_mutex m4(); int main()
{
m1.lock();
m2.lock();
m3.lock();
m4.lock(); m1.unlock();
m2.unlock();
m3.unlock();
m4.unlock(); std::cout << "hierarchy value: " << hierarchical_mutex::get_thread_hierarchy_value() << std::endl;
}

正确时,输出值应该为(unsigned long)(-1),该测试结果输出值为1000。

如果打算使用上述的类,建议修改unlock函数如下所示:

    void unlock()
{
if (this_thread_hierarchy_value != hierarchy_value)
{
throw std::logic_error("mutex unlock hierarchy violated");
}
this_thread_hierarchy_value = previous_hierarchy_value;
internal_mutex.unlock();
}

希望给看C++ Concurrent in Action一点提示。

hierarchical_mutex函数问题(C++ Concurrent in Action)的更多相关文章

  1. C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别

    以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...

  2. lr 函数--lr_save_string

    lr_eval_string   返回脚本中一个参数当前的值 Returns the string argument after evaluating embedded parameters.一般都用 ...

  3. Thinkphp内置截取字符串函数

    Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...

  4. .net mvc Authorization Filter,Exception Filter与Action Filter

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  5. 常见的transformation 和 Action

    常见transformation map 将RDD中的每个元素传入自定义函数,获取一个新的元素,然后用新的元素组成新的RDD filter 对RDD中每个元素进行判断,如果返回true则保留,返回fa ...

  6. C# 5.0 Async函数的提示和技巧

    一.创建Async函数 Async是C# 5.0中新增的关键字,通过语法糖的形式简化异步编程,它有如下三种方式: async Task<T> MyReturningMethod { ret ...

  7. Delegate,Action,Func,Predicate的使用与区别

    C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...

  8. Python基础(五)-函数

    函数: 1.定义与使用: def 函数名(参数): "函数_文档字符串" 函数体 ... return [表达式] ## def:表示函数的关键字 函数名:函数名称,根据函数名调用 ...

  9. 6.分析request_irq和free_irq函数如何注册注销中断

    上一节讲了如何实现运行中断,这些都是系统给做好的,当我们想自己写个中断处理程序,去执行自己的代码,就需要写irq_desc->action->handler,然后通过request_irq ...

随机推荐

  1. 【HDOJ1384】【差分约束+SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others)     ...

  2. 《DSP using MATLAB》Problem 5.6

    频率采样定理是这样的: 由上述定理可知,一个有限长序列(假设为N)的DTFT等间隔采样,采样数至少大于等于N. 代码: %% +++++++++++++++++++++++++++++++++++++ ...

  3. Java中动态获取项目根目录的绝对路径

    https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...

  4. python与系统做交互常用的模块和使用方法

    1.使用os模块与系统做简单命令的交互 >>>import os >>>os.popen('pwd') <open file 'pwd', mode 'r' ...

  5. oracle完全之dbf文件出现问题, ORA-01219

    alter database datafile '/data/app/oradata/ora237/users01.dbf' offline drop; 强制删除该故障文件

  6. MySQL Transaction--两阶段提交事务

    分布式事务两阶段提交 在分布式事务中,需要协调所有分布式原子事务参与者,并决定提交或回滚分布式事务,因此采用两阶段提交协议: 第一阶段为请求阶段或表决阶段,事务协调者通知事务参与者准备提交或取消事务, ...

  7. 远程连接mysql数据库碰到的问题及解决方案

    直接用SecureCRT登录到我的linux服务器,连接mysql后出现以下错误 然后我用root用户登录到mysql查看了我的用户信息,如下 出现问题1的原因有很多种,我这里是因为用错了yangli ...

  8. Behavior Designer 学习

    http://www.opsive.com/ 简单Demo Sequence Selector Chase Enemy enemy:player:

  9. phpstorm 2017激活码(方法)

    JetBrains激活 JetBrains 授权服务器(License Server URL):http://idea.imsxm.com 使用方法:激活时选择License server 填入htt ...

  10. [转]linux中vim命令

    在vi中按u可以撤销一次操作 u      撤销上一步的操作 ctrl+r 恢复上一步被撤销的操作 在vi中移动光标至: 行首:^或0 行尾:$ 页首:1G(或gg) 页尾:G(即shift+g) 显 ...