A talk C Chestnut bar (116th time: C Language Instance--mutual exclusion of thread synchronization two)

Source: Internet
Author: User
Tags mutex semaphore

Crossing, hello, everyone. What we said last time is an example of the semaphore of thread synchronization, which we continue to say. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

We described in detail the use of mutex-related functions in the previous section, and this time, we show how to use these functions to manipulate mutex amounts.

Here is the detailed procedure:

    • 1. Define a mutex, a, used to synchronize threads;
    • 2. Using the Pthread_mutex_init function to initialize the mutex in the process of creating the thread, the property of the mutex uses the default value;
    • 3. Read data in the thread that reads the data, first use the Pthread_mutex_lock function to lock the mutex a, then read the data, and finally use the Pthread_mutex_unlock function to unlock the mutex a operation;
    • 4. Modify the data in the thread that writes the data, first use the Pthread_mutex_lock function to lock the mutex a, then modify the data, and finally use the Pthread_mutex_unlock function to unlock the mutex A;
    • 5. Use the Pthread_mutex_destroy function to release the resources associated with the mutex in the process of creating the thread;

Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use.

The code we wrote was modified on the basis of the semaphore mutex code, but we used the mutex in the code instead of the semaphore. The function of reading/writing data in code is implemented by itself, in order to explain the problem conveniently, the delay operation is used in both functions, in order to explain that it takes a certain time to read or write the data.

There may be situations when the program is running:

    • The read operation is not completed, the start of the write operation, which will cause the read operation to read the data is not accurate;
    • The write operation has not been completed, start reading operation, which will cause the read operation to read the data is not accurate;

Here is the result of running the program without using mutex , please refer to:

CreateFirst thread//creation of oneCreateSecond thread//create a second threadThread ID::3076062016 -----------S----------[thread_1] Start reading  Data//The first thread starts reading (the first action on the data is a read operation)Thread ID::3067669312 -----------S----------[thread_2] Start writing  Data//second thread starts to modify[thread_1] Data = 0//The first thread reads the initial value of the shared data[thread_1] End Reading Data [thread_2]Data   = 1//The second thread modifies the shared data[thread_2] End Writing Data [thread_2] Start writingData     [thread_1] Start readingData     [thread_2] Data = 2[thread_2] End Writing Data [thread_1] Data = 2[thread_1] End Reading Data [thread_2] Start writing Data [thread_2] Data = 3[thread_2] End Writing Data [thread_1] Start reading Data [thread_2] Start writing Data [thread_1] Data = 3[thread_1] End Reading Data [thread_2] Data = 4[thread_2] End Writing Data Thread ID::3067669312 -----------E----------//end of second thread[thread_1] Start reading Data [thread_1] Data = 4[thread_1] End Reading Data Thread ID::3076062016 -----------E----------//end of the first thread

As you can see from the above results, the second thread has not finished writing the data, the first thread begins to read the data, and reads the initialization value of the shared data. It can be seen that the value he reads is not the modified data of the second thread, or the data that is not accurate. Further down, the thread that reads the data runs alternately with the thread that modifies the data, so the threads run in an incorrect order. This shows that if you do not synchronize the threads, operating on the shared data produces incorrect results.

Here are the results of running the program after using the mutex synchronization thread , please refer to:

CreateFirst thread//creation of oneCreateSecond thread//create a second threadThread ID::3075980096 -----------S----------[thread_1] Start reading  Data//The first thread starts reading (the first action on the data is a read operation)Thread ID::3067587392 -----------S----------[thread_1] Data = 0//The first thread reads the initial value of the shared data[thread_1] End ReadingData   //First thread reads shared data end[thread_2] Start writing Data //second thread starts to modify values of shared data[thread_2] Data = 1//The second thread modifies the value of the shared data[thread_2] End WritingData   //Second thread modifies the end of shared data[thread_1] Start readingData   //First thread begins to read value of shared data[thread_1] Data = 1//The first thread read the value of the correct shared data[thread_1] End ReadingData   //First thread reads shared data end[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 [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 [thread_2] Start writing Data [thread_2] Data = 4[thread_2] End Writing Data Thread ID::3075980096 -----------E----------//end of the first threadThread ID::3067587392 -----------E----------//end of second thread

As you can see from the above results, the first thread begins to read the data, the second thread begins to modify the data after it is read, and the first thread waits until the second thread modifies the data before it begins to read the data, and it reads the exact shared data. Looking down, the threads that read the data and the threads that modify the data run sequentially in order. Thus, after synchronizing the threads, the sequence of operations on the shared data is correct, and the values read from the shared data are correct. Also, compare the synchronous operation of the thread with the semaphore. The first operation on shared data is a write operation, while the first operation on shared data is read when the thread is synchronized using a mutex. In normal terms, you must modify the data before you can read the contents of the data. This shows that the semaphore is more rigorous in order to run the thread.

In our experience, semaphores are often used in cases where there are strict requirements for counting or sequencing, whereas mutexes are often used to access shared resources. Of course, in the synchronization of threads, you can according to their own needs and the requirements of the program to choose the amount of semaphores and mutexes.

You crossing, for example, about the mutex of thread synchronization we're going to talk about this. I want to know what the following example, and listen to tell.

A talk C Chestnut bar (116th time: C Language Instance--mutual exclusion of thread synchronization two)

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.