Talk C together (111st back: C language instance-Inter-thread communication)

Source: Internet
Author: User

Talk C together (111st back: C language instance-Inter-thread communication)

Hello, everyone. We talked about the thread identifier in the previous example. This example is as follows:Inter-thread Communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Seeing officers, we mentioned inter-process communication in the previous chapter. Is there any communication between threads? Strictly speaking, there is no inter-thread communication. From the perspective of communication, communication is mainly about sending/receiving signals or transmitting data. threads and processes are different in these aspects. In fact, they are determined by their characteristics. A process has its own resources. These resources are private and cannot be shared with other processes. Therefore, communication between processes is required. A thread (the same thread is used here) there is no resource of its own, because the thread is created by the same process, so these threads can use any process resources, that is, they can share the process resources, since the resources can be shared, therefore, there is no need for communication.

Next, let's take an example to illustrate how to define a global variable data in the code. Use it as a shared resource of processes and threads, modify it in different processes and threads, and display its value.

The initial value of data is 1. modify the parent process to 2 and the child process to 3. After the child process completes running, create two threads in the parent process; in the first thread, change the value of data to 4; in the second thread, change the value of data to 5. The values before and after data modification are displayed in each process and thread.

The following is the running result of the program. For details, refer:

PID: 4394-> Father Process is running // the parent Process is running the old value of data is 1 the new value of data is 2 // the parent Process changes the data value from 1 to 2PID: 4395-> Son Process is running // the child Process is running the old value of data is 1 the new value of data is 3 // the parent Process changes the value of data from 1 to 3Son. process finished: PID = 4395 Create first thread by 4394 // The ID of The first thread created by The parent process is: 3075603264 the old value of data is 2 the new value of data is 4 // the first thread changes the value of data from 2 to 4 first thread function running finished. create second thread by 4394 // after The first thread finishes running, Create The second thread The ID of thread is: 3075603264 the old value of data is 4 the new value of data is 5 // the second thread changes the value of data from 4 to 5 second thread function running finished.

From the preceding running results, we can see that:

1. The parent process and child process have data respectively. Their initial values are 1. 2. The modification of data by the parent process and child process does not affect each other. The parent process changes data from 1 to 2. The child process changes data from 1 to 3. two threads are created in the parent process. The two threads share data with the parent process, and the initial values of data are both 2. 4. the value of data is modified in the first thread, and the value of data is changed to 4, which affects the second thread. the initial value of data read by the second thread is 4, which is the value after the data is modified by the first thread;

As you can see, after the previous thread in the same process modifies the shared variable data, the last thread in the process can directly get the modified value of the previous thread. Therefore, there is no special communication mode between threads, which is why we say there is no communication between threads. In contrast, the parent process modifies the value of the shared variable data, but the child process cannot see the modification of the shared variable data by the parent process. Therefore, if you want to transmit data between processes, special communication methods are required.

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

Let's talk about the example of producers and consumers. 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.