多线程:死锁

(1)线程试图对一个互斥锁加锁两次

test:

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex;

void *thread(void *arg)
{
    pthread_mutex_lock(&mutex);    //第一次持有互斥锁
    printf("first get mutex\n");

    pthread_mutex_lock(&mutex);     //第二次试图持有互斥锁
    printf("second get mutex\n");

    pthread_mutex_unlock(&mutex);
    pthread_mutex_unlock(&mutex);
}

int main(int argc, char const *argv[])
{
    pthread_mutex_init(&mutex, NULL);
    pthread_t tid;
    pthread_create(&tid, NULL, thread, NULL);

    pthread_join(tid, NULL);

    pthread_mutex_destroy(&mutex);

    return 0;
}

在这里插入图片描述
结果分析:在线程第一次持有互斥锁后,没有释放互斥锁,若再次试图持有互斥锁,线程就被阻塞,形成死锁。

(2)线程1持有锁A,试图持有锁B;线程2持有锁B,试图持有锁A

test:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

void *thread1(void *arg)
{
    pthread_mutex_t *mutex = (pthread_mutex_t*)arg;
    //thread1持有mutex[0]
    pthread_mutex_lock(&mutex[0]); 
    sleep(1);
    int i = 1;

    while(1)
    {
        //企图还想持有mutex[1]
        if(pthread_mutex_trylock(&mutex[1]) == 0)
        {
            printf("I am thread1, I get the mutex[1]\n");
            break;
        }
        else
        {
            printf("thread1, 第%d次尝试持有mutex[1]...\n", i++);
            sleep(1);   //休眠1s,再次企图持有mutex[1]
        }
            
    }

    pthread_mutex_unlock(&mutex[0]);   //释放mutex[0]

}

void *thread2(void *arg)
{
    pthread_mutex_t *mutex = (pthread_mutex_t*)arg;
    //thread1持有mutex[1]
    pthread_mutex_lock(&mutex[1]); 
    sleep(1);
    int i = 0;

    while(1)
    {
        //企图还想持有mutex[0]
        if(pthread_mutex_trylock(&mutex[0]) == 0)
        {
            printf("I am thread2, I get the mutex[0]\n");
            break;
        }
        else
        {
            printf("thread2, 第%d次尝试持有mutex[0]...\n", i++);
            sleep(1);   //休眠1s,再次企图持有mutex[0]
        }
    }
    pthread_mutex_unlock(&mutex[1]);   //释放mutex[1]
}

int main(int argc, char const *argv[])
{
    //静态初始化互斥锁
    pthread_mutex_t mutex[2] = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER};

    pthread_t tid[2];
    //创建两个线程
    pthread_create(&tid[0], NULL, thread1, (void *)mutex);
    pthread_create(&tid[1], NULL, thread2, (void *)mutex);

    //主线程阻塞等待两个子线程
    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    //销毁互斥锁
    pthread_mutex_destroy(&mutex[0]);
    pthread_mutex_destroy(&mutex[1]);
    return 0;
}

在这里插入图片描述

(3)在一个线程拥持有互斥锁时,接收到其他线程用pthread_join发送过来的终止信号

test:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

pthread_mutex_t mutex;

void *thread1(void *arg)
{
    pthread_mutex_lock(&mutex); //上锁

    for(int i = 0; i < 5; ++i)
    {
        printf("thread1 get mutex\n");
        sleep(1);
    }
    pthread_mutex_unlock(&mutex); //释放锁

}

void *thread2(void *arg)
{
    pthread_mutex_lock(&mutex); //上锁

    printf("thread2 get mutex\n");  //由于thread1被终止,互斥锁资源被销毁,所以thread2将无法获得互斥锁

    pthread_mutex_unlock(&mutex); 
}

int main(int argc, char const *argv[])
{
    pthread_t tid[2];
    pthread_create(&tid[0], NULL, thread1, NULL);

    sleep(3);   
    pthread_cancel(tid[0]); //向thread1发送终止信号

    pthread_create(&tid[1], NULL, thread2, NULL);

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}

在这里插入图片描述
结果分析:在thread1运行3s后,主线程向thread1发送了终止信号,互斥锁资源被销毁,所以thread2再也获得不了互斥锁,一直阻塞,造成死锁。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值