linux下用C++实现死锁检测

//mutex_lock.h
#ifndef MUTEX_LOCK1_H
#define MUTEX_LOCK1_H
#include<bits/stdc++.h>
using namespace std;
//定义全局变量,记录当前等待锁资源的线程ID,以及对应这个锁已经被其他线程占用的线程ID,形成想对应的映射关系
//对于一把互斥锁,map记录<想拥有此锁的线程(当前线程),已拥有此锁的线程>
unordered_map<unsigned long,unsigned long> g_map_tid_waiters_holders;
class Mutex{
private:
    pthread_mutex_t _session;
    pthread_t _hold_tid;
public:
    Mutex(){
        _hold_tid=0;
        pthread_mutex_init(&_session,nullptr);
    }
    ~Mutex(){
        pthread_mutex_destroy(&_session);
    }
    void lock();
    void unlock();
private:
    Mutex(const Mutex &);
    void operator=(const Mutex&);
    bool RecurCheckDeadLock(unsigned long,unsigned long,unsigned long,
    unordered_map<unsigned long,unsigned long>&);
};
void Mutex::lock(){
    //锁已经被占用,需要等待而无法进入了,如果锁被当前线程占用,那么依旧可以重入(递归锁),也就是一个线程可以多次调用lock()函数而不会阻塞
    if(_hold_tid!=0&&_hold_tid!=pthread_self()){
        cout<<"current thread:"<<pthread_self()<<" wait to get mutex locked by thread:"<<_hold_tid<<endl;
        g_map_tid_waiters_holders[pthread_self()]=_hold_tid;//记录抢占资源的关系
    }
    auto map_tid_waiters_holders=g_map_tid_waiters_holders;//当前线程拥有的资源
    if(RecurCheckDeadLock(pthread_self(),pthread_self(),_hold_tid,map_tid_waiters_holders)){
        cout<<"current thread:"<<pthread_self()<<"   deadlock"<<endl;
    }
    pthread_mutex_lock(&_session);//当其他线程获取不到锁时会阻塞
    _hold_tid=pthread_self();
    for(auto it=g_map_tid_waiters_holders.begin();it!=g_map_tid_waiters_holders.end();it++){
        if(_hold_tid==it->first){
            g_map_tid_waiters_holders.erase(it);//已经成功获取了锁 如果有映射关系解除相应的映射关系
            break;
        }
    }
}
void Mutex::unlock(){
    pthread_mutex_unlock(&_session);
    _hold_tid=0;
}
bool checkdeadlock(unsigned long thread_id,unordered_map<unsigned long ,unsigned long>& map_tid_waiters_holders){
    unsigned long key=thread_id;
    auto it=map_tid_waiters_holders.begin();
    while(key!=it->second){
        
    }
}
bool Mutex::RecurCheckDeadLock(
    unsigned long first_key,
    unsigned long key,
    unsigned long value,
    unordered_map<unsigned long ,unsigned long>& map_tid_waiters_holders){
        bool ret=false;
        for(auto it=map_tid_waiters_holders.begin();it!=map_tid_waiters_holders.end();it++){
           if(key==it->first&&value==it->second){//当前键值关系是当前的  key== 前一层关系的value
              map_tid_waiters_holders.erase(it);
              break;
            }
       }
       for(auto it=map_tid_waiters_holders.begin();it!=map_tid_waiters_holders.end();it++){
           if(it->first==value){
               if(first_key==it->second)//当前键值关系的value 就是最开始自己想找的    A出发最后总算找到了A
                return true;
                return RecurCheckDeadLock(first_key,it->first,it->second,map_tid_waiters_holders);//继续下一层查找
           }
       }
        return ret;
    }
#endif
//main.cpp
#include"mutex_lock1.h"
#include<bits/stdc++.h>
#include<unistd.h>
Mutex mtx1;
Mutex mtx2;
void* thread1(void*){
    mtx1.lock();
    cout<<"thread 1 is working"<<endl;
    sleep(1);
    mtx2.lock();

    mtx2.unlock();
    mtx1.unlock();
    pthread_exit(nullptr);
}
void* thread2(void*){
    mtx2.lock();
    cout<<"thread 2 is working"<<endl;
    sleep(1);
    mtx1.lock();

    mtx1.unlock();
    mtx2.unlock();
    pthread_exit(nullptr);
}
int main(){
    cout<<getpid()<<endl;
    pthread_t tid[2];
    if(pthread_create(&tid[0],nullptr,thread1,nullptr)==-1){
        perror("pthread create fail");
        exit(-1);
    }
    if(pthread_create(&tid[1],nullptr,thread2,nullptr)==-1){
        perror("pthread create fail");
        exit(-1);
    }
    pthread_join(tid[0],nullptr);
    pthread_join(tid[1],nullptr);
    exit(0);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值