hierarchical_mutex函数问题(C++ Concurrent in Action)
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)的更多相关文章
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- lr 函数--lr_save_string
lr_eval_string 返回脚本中一个参数当前的值 Returns the string argument after evaluating embedded parameters.一般都用 ...
- Thinkphp内置截取字符串函数
Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...
- .net mvc Authorization Filter,Exception Filter与Action Filter
一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...
- 常见的transformation 和 Action
常见transformation map 将RDD中的每个元素传入自定义函数,获取一个新的元素,然后用新的元素组成新的RDD filter 对RDD中每个元素进行判断,如果返回true则保留,返回fa ...
- C# 5.0 Async函数的提示和技巧
一.创建Async函数 Async是C# 5.0中新增的关键字,通过语法糖的形式简化异步编程,它有如下三种方式: async Task<T> MyReturningMethod { ret ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- Python基础(五)-函数
函数: 1.定义与使用: def 函数名(参数): "函数_文档字符串" 函数体 ... return [表达式] ## def:表示函数的关键字 函数名:函数名称,根据函数名调用 ...
- 6.分析request_irq和free_irq函数如何注册注销中断
上一节讲了如何实现运行中断,这些都是系统给做好的,当我们想自己写个中断处理程序,去执行自己的代码,就需要写irq_desc->action->handler,然后通过request_irq ...
随机推荐
- XML映射配置文件
XML映射配置文件 http://www.mybatis.org/mybatis-3/configuration.html Type Handlers 类型处理器 每当MyBatis在Prepared ...
- 05 ajax,jquery,xstream,json解析
Ajax “Asynchronous Javascript And XML”(异步JavaScript和XML),把原有的技术,整合到一起 1.使用CSS和XHTML来表示. ...
- oracle ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库(转载+自己经验总结)
字符集子集向其超集转换是可行的,如此例 ZHS16GBK转换为AL32UTF8. 导出使用的字符集将会记录在导出文件中,当文件导入时,将会检查导出时使用的字符集设置,如果这个字符集不同于导入客户端的N ...
- WordPress博客插入直播源
方法很简单: 找到直播源地址,撰写新文章(可视化切换到文本模式下)插入直播源地址 代码:<iframe id="tv_iframe" width="880" ...
- loopback 代码解析
loopback-boot boot(app, __dirname);//server.js var instructions = compile(options); execute(app, ins ...
- day2 python学习
1.格式化输出 现有一练习需求,问用户的姓名.年龄.工作.爱好 ,然后打印成以下格式 复制代码 ------------ info of Alex Li ----------- Name : Alex ...
- nakadi-ui nakadi event broker 的可视化UI工具
nakadi 是一款很不错的基于fafka 开发的event broker ,我们只需要使用http 请求就可以调用kafka 方便的发布订阅功能 环境准备 docker-compose 文件 ver ...
- Mosaic 前端微服务框架
Mosaic 是一系列的服务.库,集成在一起,定义了组件如何彼此交互,可以用来支持大规模的web 站点开发 一张架构图 说明 尽管上图中的一些组件已经迭代演化了(skipper 的route 配置,上 ...
- sql里 where和order by一起使用是怎样的顺序
where 列2 = ‘条件1’ 这个先执行过滤后的数据 再order by ‘条件2’最后取第一条数据也就是先where 再order by 再limit
- Linux 定时任务crontab使用
正好要搞一个定时删除数据的功能,想到linux 可设置定时器定时执行任务就学习了下~ 并不是所有的linux服务器上都装了crontab 像我这: [root@hehe local]# crontab ...