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. hdu3974 Assign the task dfs序+线段树

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  2. Eclipse maven 错误修正方法:An error occurred while filtering resources

    最近打开Eclipse后发现项目报红叉,解决办法如下: 1.eclipse中删除该项目(注意:不要删除代码) 2.cmd,进入到项目目录下,执行命令:mvn eclipse:clean 3.重新导入项 ...

  3. Java 如何实现线程间通信?(notify、join、CountdownLatch、CyclicBarrier、FutureTask、Callable )

    转自:https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247486499&idx=1&sn=d3f2d6959df ...

  4. python 基础的用法新发现

    引用的发现: # 引用的处理 因为变量指的是某一内存地址 引用变量实际是引用一个固定的内存地址,# 当这个变量中的内容变了之后,他的内存地址不变, 所以引用者也动态的得到了变化的变量l1=[1,2,3 ...

  5. CDH5上安装Hive,HBase,Impala,Spark等服务

    Apache Hadoop的服务的部署比較繁琐.须要手工编辑配置文件.下载依赖包等.Cloudera Manager以GUI的方式的管理CDH集群,提供向导式的安装步骤.因为须要对Hive,HBase ...

  6. python 简明教程 【转】

    转自:https://learnxinyminutes.com/docs/python/ # Single line comments start with a number symbol. &quo ...

  7. Spring事务传播属性和隔离

     1 事务的传播属性(Propagation)  1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if no ...

  8. ik_max_word ik_smart 区别 和 单字 查询 不到问题

    ik_smart:分词的时候只分一次,句子里面的每个字只会出现一次. 比如:中华人民共和国国歌 入上图,分成:中华人民共和国 国歌 2 部分.每个字都自出现了一次.(我指的每一个位置 的子. 2个国 ...

  9. Iris分类以及数组reshape想到的

    最近在研究Iris花的逻辑回归分类中看到了如下的代码: from sklearn.linear_model import LogisticRegression X = iris["data& ...

  10. C# 值传参和引用传参

    值传递:对于个人理解,值传递就像是python中的深拷贝,值传递的内容和原来的内容是两份不同的东西,相互不影响. 引用传递:对于个人理解,引用传递就是python中的复杂多维list的浅拷贝,它传递的 ...