Talk C chestnuts together (118th back: C language instance -- thread deadlock 2)

Source: Internet
Author: User

Talk C chestnuts together (118th back: C language instance -- thread deadlock 2)

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!

We introduced the concept of deadlocks in the last session, analyzed the cause of deadlocks in detail, and provided pseudocode to demonstrate deadlocks. Today, we will convert 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:

// 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_value); if (res! = 0) {printf ("mutex lock failed \ n") ;}# endif read_data ("Thread_1 "); // This Part of the lock code is newly added based on the original code, indicating the second lock on the mutex # if MUTEX_ENABLE res = pthread_mutex_lock (& mutex_value); if (res! = 0) {printf ("mutex lock failed \ n") ;}# endif # if MUTEX_ENABLE res = pthread_mutex_unlock (& mutex_value); if (res! = 0) {printf ("mutex unlock failed \ n") ;}# endif sleep (2);} printf ("Thread ID: % u ----------- E ---------- \ n ", (unsigned int) thread_id); pthread_exit (& status); // end the thread}

The following is the running result of the program.For more information, see:

Create first thread // Create the first thread Create second Thread thread ID: 3076418368 ----------- S ---------- [Thread_1] start reading data // The first thread runs and starts reading data. Lock Thread ID: 3068025664 ------------- S ---------- [Thread_1] data = 0 [Thread_1] end reading data before reading data // The first Thread stops reading data, next, lock the mutex for the second time. After the deadlock mutex can't be destroyed // occurs in the program, the resources related to the mutex cannot be correctly released.

From the preceding program running results, we can see that the program has a deadlock, and the threads for reading and modifying data are not running properly, and there are also running errors. It can be seen that the deadlock has serious consequences.

Next we will demonstrate how to use the pthread_mutex_trylock function to avoid deadlocks. We also use the code in the above program. We only need to replace the pthread_mutex_lock function in the Code with the pthread_mutex_trylock function. Other code remains unchanged.

The following is the program running result after the code is modified::

Create first thread // Create the first thread Create second Thread // Create the second thread ID: 3076127552 ----------- S ---------- [Thread_1] start reading data // run the first Thread, and start reading data. Lock Thread ID for mutex before reading data :: 3067734848 ----------- S ---------- [Thread_1] data = 0 [Thread_1] end reading data mutex lock failed // The first thread reads data and then locks the mutex for the second time, the lock fails. [Thread_2] start writing data [Thread_2] data = 1 [Thread_2] end writing data [Thread_1] start reading data [Thread_1] data = 1 [Thread_1] end reading data mutex lock failed // The lock fails every time the lock is repeated, but there will be no deadlock [Thread_2] start writing data [Thread_2] data = 2 [Thread_2] end writing data [Thread_1] start reading data [Thread_1] data = 2 [Thread_1] end reading data mutex lock failed [Thread_2] start writing data [Thread_2] data = 3 [Thread_2] end writing data [Thread_1] start reading data [Thread_1] data = 3 [Thread_1] end reading data mutex lock failed [Thread_2] start writing data [Thread_2] data = 4 [Thread_2] end writing data Thread ID:: 3076127552 ----------- E ---------- // The first Thread Ends Thread ID: 3067734848 ----------- E ---------- // The second Thread ends

From the preceding program running result, we can see that no deadlock occurs when the second lock is applied to the mutex, but the lock fails. At this time, the program runs normally, the threads that read and modify shared data run normally until the end of the process. We can see that the pthread_mutex_trylock function is used to avoid deadlocks.

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.