接上一篇:死锁:解决方案(1)
方案5:使用锁层次。虽然实际上是定义锁定顺序的一个特例,但锁层次能够提供一种方法,来检查在运行时是否遵循了约定。其思路是将应用程序分层,并且能够在任意层级上锁定互斥元。当代码试图锁定一个互斥元时,如果它在较低层已经持有锁定,那么就不允许它锁定该互斥元。通过给每一个互斥配层号,并记录下每个线程都锁定了哪些互斥元,这样就可以在运行时进行检查了。代码如下所示:
#include <thread>
#include <mutex>
#include <stdio.h>
#include <unistd.h>
class hierarchical_mutex
{
private:
std::mutex internal_mutex;
unsigned long const hierarchy_value;
unsigned long previous_hierarchy_value;
static thread_local unsigned long this_thread_hierarchy_value;//使用thread_local关键字来保存当前线程的层次值,每个线程都有自己的副本
void check_for_hierarchy_violation()
{
printf("%s\r\n",__func__);
if(this_thread_hierarchy_value <= hierarchy_value)
{
throw std::logic_error("mutex hierarchy violated");
}
}
void update_hierarchy_value() //如果lock成功了,保存当前的层次值,以便在unlock中恢复之前的层次值
{
printf("%s\r\n",__func__);
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(0)
{}
void lock()
{
printf("%s\r\n",__func__);
check_for_hierarchy_violation();
internal_mutex.lock();
update_hierarchy_value();
}
void unlock()
{
printf("%s\r\n",__func__);
this_thread_hierarchy_value = previous_hierarchy_value;
internal_mutex.unlock();
}
bool try_lock()
{
printf("%s\r\n",__func__);
check_for_hierarchy_violation();
if(!internal_mutex.try_lock()) return false;
update_hierarchy_value();
return true;
}
};
thread_local unsigned long hierarchical_mutex::this_thread_hierarchy_value(90000);//当线程第一次锁定hierarchical_mutex的实例时,this_thread_hierarchy_value的值为最大值90000,其本质是让第一次的check_for_hierarchy_violation通过。
hierarchical_mutex high_level_mutex(10000);
hierarchical_mutex low_level_mutex(5000);
int do_low_level_stuff()
{
while(1)
{
printf("%s\r\n",__func__);
sleep(1);
}
return 0;
}
int low_level_func()
{
printf("%s\r\n",__func__);
std::lock_guard<hierarchical_mutex> lk(low_level_mutex);
return do_low_level_stuff();
}
void high_level_stuff(int some_param)
{
while(1)
{
printf("%s\r\n",__func__);
sleep(1);
}
}
void high_level_func()
{
printf("%s\r\n",__func__);
std::lock_guard<hierarchical_mutex> lk(high_level_mutex);
high_level_stuff(low_level_func());
}
void thread_a_callback()
{
printf("%s\r\n",__func__);
high_level_func();
}
hierarchical_mutex other_mutex(100);
void do_other_stuff()
{
printf("%s\r\n",__func__);
high_level_func();
}
void other_stuff()
{
printf("%s\r\n",__func__);
high_level_func();
do_other_stuff();
}
void thread_b_callback()
{
printf("%s\r\n",__func__);
std::lock_guard<hierarchical_mutex> lk(other_mutex);
other_stuff();
}
int main()
{
std::thread thread_a(thread_a_callback);
sleep(5);
std::thread thread_b(thread_b_callback);
thread_a.join();
thread_b.join();
return 1;
}
如代码所示:thread_a遵循了规则,所以它将会运行良好。thread_a调用了high_level_func(),它锁定了high_level_mutex(具有层次值10000), 并接着使用了这个锁定了的互斥元调用了low_level_func()。low_level_func()接着锁定了low_level_mutex,但是没关系,因为该互斥元具有较低的层次值(5000)。而thread_b无视了规则,因此将在运行时失败。刚开始它锁定了other_mutex,它具有的层次值仅为100。当other_stuff()调用high_level_func()时,试图获取什为10000的high_level_mutex,大大超过100的当前层次值,因此触发hierarchical_mutex的异常。
运行效果如下所示: