• 一起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---------- 
    ",(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 
    ");
            }
    #endif
            read_data("Thread_1");
    
    #if MUTEX_ENABLE
            res = pthread_mutex_lock(&mutex_value2); //mutex2 is locking
            if(res != 0)
            {
                printf(" mutex2 lock failed 
    ");
            }
    #endif
    
    #if MUTEX_ENABLE
            res = pthread_mutex_unlock(&mutex_value2);
            if(res != 0)
            {
                printf(" mutex2 unlock failed 
    ");
            }
    
            res = pthread_mutex_unlock(&mutex_value1);
            if(res != 0)
            {
                printf(" mutex1 unlock failed 
    ");
            }
    #endif
            sleep(2);
        }
    
        printf("Thread ID::%u -----------E---------- 
    ",(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---------- 
    ",(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 
    ");
            }
    #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 
    ");
            }
    #endif
    
    #if MUTEX_ENABLE
            res = pthread_mutex_unlock(&mutex_value1);
            if(res != 0)
            {
                printf(" mutex1 unlock failed 
    ");
            }
    #endif
    
    #if MUTEX_ENABLE
            res = pthread_mutex_unlock(&mutex_value2);
            if(res != 0)
            {
                printf(" mutex2 unlock failed 
    ");
            }
    #endif
            sleep(1);
        }
    
        printf("Thread ID::%u -----------E---------- 
    ",(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锁住了相互排斥量二,同一时候等待相互排斥量一。这样便造成了死锁,进而引起了程序执行错误。

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

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

    各位看官,关于线程死锁的样例咱们就讲到这里。

    欲知后面还有什么样例,且听下回分解 。


  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    day 47 前端基础之BOM和DOM
    day 46 Javascript学习
    day 45 前端CSS
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7082295.html
Copyright © 2020-2023  润新知