Talk C chestnuts together (119th back: C language instance -- thread deadlock 3)

Source: Internet
Author: User

Talk C chestnuts together (119th back: C language instance -- thread deadlock 3)

Hello, the last time we talked aboutThread deadlock. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

For the reason of the length, we only introduced the first cause of the deadlock in the last time. Today we will introduce the second cause of the deadlock, andConvert pseudo-code to the actual C language code.

For convenience, we use the mutex code described in the previous chapter back, and make some minor modifications based on the Code to demonstrate the deadlock. The Code is as follows:

First, define two mutex. The mutex is a global variable for convenient use by the thread.

#if MUTEX_ENABLEpthread_mutex_t mutex_value1;pthread_mutex_t mutex_value2;#endif

Next, initialize two mutex values in the main process (that is, the main function:

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

At the end of the main process, remember to release resources related to mutex:

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

We modify the execution functions of two threads respectively. The code in this section is the core code. Please read it carefully:

// the first thread functionvoid *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 functionvoid *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}

Run the above program to get the following results:

Create first thread // Create the first thread Create second Thread // Create the second thread ID: 3076344640 ----------- S ---------- [Thread_1] start reading data // The first Thread reads data, at the same time, lock Thread ID: 3067951936 ----------- S ---------- [Thread_2] start writing data // modify data in the second Thread, meanwhile, lock the mutex [Thread_1] data = 0 [Thread_1] end reading data // The first thread stops reading data, at the same time, wait until mutex 2 is unlocked [Thread_2] data = 1 [Thread_2] end writing data // The second thread ends modifying the data, at the same time, wait for mutex2 can't be destroyed when the mutex is unlocked. // The program is running incorrectly.

From the preceding program running results, we can see that thread 1 locks mutex 1 and waits for mutex 2. Thread 2 locks mutex 2 and waits for mutex 1. In this way, a deadlock occurs and the program running error occurs.

This program is specially written to demonstrate the cause of the deadlock. It is unreasonable to write the program because it cannot synchronize threads. Do not stick to the program content, but focus on understanding how the deadlock occurs.

The readers will not write code in the text, and the completed Code will be put into my resources. You can download and use it.

Let's talk about the example of thread deadlock. I want to know what examples will be provided later, and I will try again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.