一起talk C栗子吧(第一百一十九回:C语言实例--线程死锁三)


各位看官们,大家好,上一回中咱们说的是线程死锁的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起talk C栗子吧!

看官们,由于篇幅的原因我们在上一回只介绍了死锁发生的第一种原因,今天我们将介绍死锁发生的第二种原因,并且该原因中的伪代码转换为实际的C语言代码

为了方便,我们使用前面章回中演示互斥量的代码,在该代码的基础上做一些小修改来演示死锁。代码如下:

首先定义两个互斥量,互斥量是全局变量,方便线程使用。

#if MUTEX_ENABLE
pthread_mutex_t mutex_value1;
pthread_mutex_t mutex_value2;
#endif

接下来在主进程中(也就是main函数)初始化两个互斥量:

    res = pthread_mutex_init(&mutex_value1,NULL);
    res = pthread_mutex_init(&mutex_value2,NULL);

在主进程的最后还要记得释放与互斥量相关的资源:

#if MUTEX_ENABLE
    //destroy mutex
    res = pthread_mutex_destroy(&mutex_value1);
    res = pthread_mutex_destroy(&mutex_value2);
#endif

我们分别修改两个线程的执行函数,该段代码是核心代码,请大家仔细阅读:

// the first thread function
void *thread_func1(void *param)
{
    int i = 0;
    int res = 0;
    pthread_t thread_id;

    thread_id = pthread_self();
    printf("Thread ID::%u -----------S---------- \n",(unsigned int)thread_id);

    while(i++ < 4)
    {
#if MUTEX_ENABLE
        res = pthread_mutex_lock(&mutex_value1);       // mutex1 is locking
        if(res != 0)
        {
            printf(" mutex1 lock failed \n");
        }
#endif
        read_data("Thread_1");

#if MUTEX_ENABLE
        res = pthread_mutex_lock(&mutex_value2); //mutex2 is locking
        if(res != 0)
        {
            printf(" mutex2 lock failed \n");
        }
#endif

#if MUTEX_ENABLE
        res = pthread_mutex_unlock(&mutex_value2);
        if(res != 0)
        {
            printf(" mutex2 unlock failed \n");
        }

        res = pthread_mutex_unlock(&mutex_value1);
        if(res != 0)
        {
            printf(" mutex1 unlock failed \n");
        }
#endif
        sleep(2);
    }

    printf("Thread ID::%u -----------E---------- \n",(unsigned int)thread_id);
    pthread_exit(&status); // end the thread
}
// the second thread function
void *thread_func2(void *param)
{
    int i = 0;
    int res = 0;
    pthread_t thread_id;

    thread_id = pthread_self();
    printf("Thread ID::%u -----------S---------- \n",(unsigned int)thread_id);

    while(i++ < 4)
    {
#if MUTEX_ENABLE
        res = pthread_mutex_lock(&mutex_value2);  //mutex 2 is locking
        if(res != 0)
        {
            printf(" mutex2 lock failed \n");
        }
#endif
        write_data("Thread_2");

#if MUTEX_ENABLE
        res = pthread_mutex_lock(&mutex_value1); //mutex 1 is locking
        if(res != 0)
        {
            printf(" mutex1 lock failed \n");
        }
#endif

#if MUTEX_ENABLE
        res = pthread_mutex_unlock(&mutex_value1);
        if(res != 0)
        {
            printf(" mutex1 unlock failed \n");
        }
#endif

#if MUTEX_ENABLE
        res = pthread_mutex_unlock(&mutex_value2);
        if(res != 0)
        {
            printf(" mutex2 unlock failed \n");
        }
#endif
        sleep(1);
    }

    printf("Thread ID::%u -----------E---------- \n",(unsigned int)thread_id);
    pthread_exit(&status); // end the thread
}

我们运行上面的程序,可以得到以下结果:

Create first thread     //创建第一个线程
Create second thread    //创建第二个线程
Thread ID::3076344640 -----------S---------- 
[Thread_1] start reading data  //第一个线程读取数据,同时对互斥量一加锁
Thread ID::3067951936 -----------S---------- 
[Thread_2] start writing data  //第二个线程修改数据,同时对互斥量二加锁
[Thread_1] data = 0 
[Thread_1] end reading data    //第一个线程读取数据结束,同时等待互斥量二被解锁
[Thread_2] data = 1 
[Thread_2] end writing data    //第二个线程修改数据结束,同时等待互斥量一被解锁
mutex2 can't be destroyed      //发生死锁,程序运行错误

从上面的程序运行结果中可以看到,线程1锁住了互斥量一,同时等待互斥量二;而线程2锁住了互斥量二,同时等待互斥量一。这样便造成了死锁,进而引起了程序运行错误。

该程序是为了演示死锁的原因专门写的,这样写程序不合理,因为它不能同步线程,大家不要拘泥于程序的内容,重点是理解死锁是如何发生的。

看官们,正文中就不写代码了,完成的代码放到了我的资源中,大家可以点击这里下载使用。

各位看官,关于线程死锁的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解 。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值