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 ...
随机推荐
- C++学习(三十)(C语言部分)之 栈和队列
数据结构1.保存数据 2.处理数据数组+操作增查删改 栈和队列是一种操作受限的线性表 栈 是先进后出 是在一端进行插入删除的操作--->栈顶 另一端叫做栈底(栈和栈区是两个概念)(是一种数据结构 ...
- YIT-CTF—Web
一:背后 打开传送门——>查看网页源代码——>1b0679be72ad976ad5d491ad57a5eec0——>用MD5解密 二:一种编码 [][(![]+[])[+[]]+([ ...
- jsp中引入jquery报错:Failed to load resource: the server responded with a status of 404 (Not Found)
问题描述: 今天自己在搭建spring.springMVC.hibernate框架,搭建完成后,在引入jquery时,发现jquery不管用.我的解决顺序是: 1.检查路径,发现路径没错,另外需要注意 ...
- CTEX(LaTeX) 编译 中文
CTEX 中文编码&编译问题 #win10 tex 文档为 GBK 编码 https://zhidao.baidu.com/question/93645685.html \documentcl ...
- Connecting Elixir Nodes with libcluster, locally and on Kubernetes
转自:https://www.poeticoding.com/connecting-elixir-nodes-with-libcluster-locally-and-on-kubernetes/ Tr ...
- ASP.NET Core WebApi使用Swagger生成api说明文档
1. Swagger是什么? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件 ...
- 996.icu 事件后
996.icu 事件后 雇主 更加关注效率 减少成本 分工再细化 精简人员 雇员 法律意识加强 个人权利争取 效率低者下岗 技能提高 效率提高 影响 商业社会更有效率 人力市场竞争更加激烈 国内竞争力 ...
- [转]web.xml中<url-pattern>详解
标签<url-pattern><url-pattern>是我们用Servlet做Web项目时需要经常配置的标签,例: <servlet> <servlet-n ...
- nginx 学习资料
nginx 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-type( ...
- zookeeper 启动显示started,jps查看进程却没有,解决方法
如图所示, 输入 zkServer.sh start 显示如下: Starting zookeeper ... STARTED 可是输入jps指令 却找不到zookeeper进程 去zkdata文件目 ...