Process and thread knowledge Review

Source: Internet
Author: User


Processes and threads

Process and thread are two opposite concepts. Generally, a process can define an instance of the program ). In Win32, a process does not execute anything. It only occupies the address space used by the application. In order for a process to complete some work, the process must have at least one thread, which is responsible for containing the Code in the process address space. In fact, a process can contain several threads that can simultaneously execute code in the process address space. To do this, each thread has its own set of CPU registers and stacks. Each process has at least one thread executing code in its address space. If no thread executes the code in the process address space, the process will not continue to exist. The system will automatically clear the process and its address space.


Implementation principle of Multithreading

When a process is created, its first thread is called the primary thread, which is automatically generated by the system. The main thread can then generate additional threads, and these threads can generate more threads. When running a multi-threaded program, these threads seem to be running at the same time. This is not the case. To run all these threads, the Operating System Schedules CPU time for each independent thread. The single-CPU operating system provides the quantum to the thread in rotation mode. After each thread uses the time slice, it submits the control. The system then allocates the CPU time slice to the next thread. Because each time slice is short enough, this gives an illusion that these threads are running at the same time. The only purpose of creating an extra thread is to take advantage of the CPU time as much as possible.



The use of multi-threaded programming can bring great flexibility to programmers, but also make it easy to solve problems that originally require complex skills. However, we should not artificially split the program into fragments so that the fragments can be executed according to their respective threads. This is not the correct way to develop the application. A thread is useful, but when a thread is used, a new problem may occur while solving the old problem. For example, you need to develop a word processing program and run the printing function as a separate thread. This sounds like a good idea, because when printing, the user can immediately return and start editing the document. In this way, the data in the document may be modified when the document is printed, and the printed result is no longer the expected content. Maybe it is better not to put the printing function in a separate thread, but if you must use multiple threads, you can also consider the following solution: the first method is to lock the printed document, let the user edit other documents so that the document will not be modified before printing ends; another method may be more effective, that is, the document can be copied to a temporary file, print the content of this temporary file and allow users to modify the original file. When the temporary file containing the document is printed, delete the temporary file.

From the above analysis, we can see that multithreading may also bring new problems while helping solve the problem. Therefore, it is necessary to find out when multithreading is required and when multithreading is not required. In general, multithreading is often used for background computing or logic judgment while operating at the front end. For GUI (graphical user interface), apart from the Development of MDI (Multi-Document Interface) do not use multithreading unless necessary.


Thread Classification

In MFC, threads are divided into two types: worker threads and user interface threads. If a thread only completes background computing and does not need to interact with users, you can use a working thread. If you need to create a thread to process the user interface, you should use the user interface thread. The main difference between the two is that the MFC framework will add a message loop to the user interface thread so that the user interface thread can process messages in its own message queue. In this case, if you need to perform some simple calculations (such as recalculating workbooks) in the background, you should first consider using the working thread, when the background thread needs to handle complicated tasks, specifically, when the execution process of the background thread changes with the actual situation, the user interface thread should be used, in order to respond to different messages.


Thread priority

When the system needs to execute multiple processes or threads at the same time, it sometimes needs to specify the thread priority. The thread priority generally refers to the base priority of the thread, that is, the combination of the relative priority of the thread to the current process and the priority of the Process containing this thread. The operating system schedules all active threads based on the priority. Each thread in the system is assigned a priority ranging from 0 to 31. During running, the system simply allocates CPU time to the first thread with a priority of 31. After the time slice of the thread ends, the system allocates CPU time to the thread with a priority of 31. When there is no thread with a priority of 31, the system will allocate CPU time to the thread with a priority of 30, and so on. In addition to changing the priority of threads in a program, the system automatically and dynamically changes the priority of threads during program execution to ensure that the system is highly responsive to end users. For example, when you press a key on the keyboard, the system will temporarily increase the priority of the thread that processes the wm_keydown message by 2 to 3. The CPU executes the thread based on a complete time slice. After the time slice is executed, the priority of the thread is reduced by 1.


Thread Synchronization

When using multi-threaded programming, a very important issue is thread synchronization. The so-called thread synchronization refers to the ability of threads to avoid damages to their data when communicating with each other. The synchronization problem is caused by the CPU time slice allocation method of the Win32 system mentioned earlier. Although at a certain time point, there is only one thread occupying the CPU (single CPU) time, but there is no way to know when and where the thread is interrupted, in this way, it is especially important to ensure that the data between threads does not break each other. In MFC, four synchronization objects can be used to ensure simultaneous running of multiple threads. They are the critical section object (ccriticalsection), the mutex object (cmutex), the semaphore object (csemaphore), and the event object (cevent ). Among these objects, the critical zone object is the easiest to use. Its disadvantage is that it can only synchronize threads in the same process. In addition, there is also a basic method, called the linearity method in this article, that is, write operations on certain data in the programming process are completed in one thread. In this way, because the code in the same thread is always executed in order, it is impossible to rewrite data at the same time.

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: 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.