Multi-task and multi-threaded 32-bit operating systems in Windows. To understand the difference between the single thread and more, you must first understand the difference between files, processes, and threads. File: An application is called file or program when there is no call (not loaded into memory). Process: The application is loaded into the memory and then called the process. Thread: The process is called thread when processed by the CPU. The Windows 32-bit operating system allows the CPU to handle different threads within a different time period, which can come from a process (multi-thread process) or from different processes. Since this time period is short, people think that in a longer period of time, the CPU is executing multiple tasks, so the Windows 32-bit operating system has a multi-threaded multitasking. When doing a program, you can call multiple threads, this is called multithreading process, or you can call multiple threads called single-threaded processes.
Talking about WINDOWS Multi-threaded Programming Nanjing University of Aeronautics and Aerospace Niu Xinzhuang 01-6-28 11:01:15
Process and thread
Process and threads are two relative concepts, usually, one process can define an instance of the program (Instance). In WIN32, the process does not perform what, it is just the address space used by the application. In order to make the process complete a certain job, the process must have at least one thread, which is the thread responsible for inclosing the code in the process address space. In fact, a process can include several threads that can execute code in the process address space simultaneously. In order to do this, each thread has its own set of CPU registers and stacks. There is at least one thread in each process to execute the code in its address space. If there is no reason in the process address space in the process, the process will not continue to exist, the system will automatically clear the process and its address space.
Multi-thread implementation principle
When you create a process, its first thread is called the primary thread, which is automatically generated by the system. You can then generate additional threads by this main line, and these threads can generate more threads.
When running a multi-threaded program, look from the surface, these threads seem to run at the same time. And the actual situation is not the case, in order to run all these threads, the operating system arranges some CPU time for each standalone thread. The single CPU operating system provides a time slice (quantum) in a rotation manner, each thread, and the system is used to assign the CPU time slice to the next thread. Since each time piece is short enough, it gives people a description of these threads. The only purpose of creating an additional thread is to take advantage of CPU time as much as possible.
Multi-threaded problem
Using multi-threaded programming can bring great flexibility to programmers, but also make it easy to make the problem that the complex tips can be solved. However, it should not be artificially divided into some fragments, allowing these debris to execute according to their respective threads, which is not the correct way to develop applications.
Threads are useful, but when using threads, it may generate new problems while solving the old problems. For example, you want to develop a word handler and want the print function as a separate thread yourself. This sounds very good idea, because when printing, the user can return immediately and start editing the document. However, in this document, the data in the document may be modified when the document is printed, and the result of printing is no longer the desired content. Perhaps it is best not to put the print function in a separate thread, but if you must use a multi-thread, you can also consider solving the following method: The first method is to lock the document being printed, let the user edit other documents, This document does not make any modifications before ending the print; another method may be more effective, you can copy the document into a temporary file, print the content of this temporary file, while allowing the user to modify the original document . When the temporary file printing of the document is completed, then this temporary file is deleted. It can be seen from the above analysis that multi-threads may also bring new problems while helping the problem. Therefore, it is necessary to figure out and when you need to create multi-threads, when you don't need multiple threads. In general, multi-threading is often used to perform the calculation or logical judgment of the background while operating, and for the GUI (graphical user interface), in addition to the development of MDI (multi-document interface) applications, we should try Do not use multithreaded.
Thread classification
In the MFC, threads are divided into two categories, namely the working thread and user interface thread. If a thread completes the background calculation, you don't need to interact with the user, you can use the working thread; if you need to create a thread that handle the user interface, you should use the user interface thread. The main difference between the two is that the MFC framework adds a message loop to the user interface thread so that the user interface thread can handle the messages in the message queue. In this way, if you need some simple computing in the background (such as the return to the spreadsheet), you should first consider using the working thread, and the post-standing thread needs to handle more complex tasks, exactly, when The execution process changes with the actual situation, you should use the user interface thread to respond to different messages.
Thread priority
The priority of the specified thread is sometimes required when the system needs to perform multiple processes or multiple threads simultaneously. The priority of the thread generally refers to the base priority of this thread, that is, the combination of thread relative to the relative priority of the process and the priority of the process of the process containing this thread. The operating system arranges all active threads based on priority, and each thread of the system is assigned a priority, and the priority ranges from 0 to 31. When running, the system simply assigns the CPU time to the first priority of 31, and after the end of the thread is completed, the system assigns the CPU time to the next priority 31. When there is no thread with a priority 31, the system will start to assign CPU time to threads having a priority 30, and push it. In addition to changing the priority of threads in the program, sometimes the system will automatically change the priority of the thread during the execution process, which is to ensure the highness of the system on the end user. For example, when the user presses a key on the keyboard, the system will temporarily increase the priority of the thread of the WM_KEYDOWN message by 2 to 3. The CPU performs threads in a complete time film. When the time is completed, the system will reduce the priority of the thread.
Synchronization of thread
There is also a very important issue when using multi-threaded programming, thread synchronization. The so-called thread synchronization means that the thread avoids the ability to destroy their respective data between communication between threads. The synchronization problem is caused by the CPU time chip allocation of the Win32 system as mentioned. Although only one thread occupies the CPU (single CPU) time, there is no way to know when, where the thread is interrupted, so how to ensure that the thread does not destroy each other is very important. In the MFC, four synchronization objects can be used to ensure multithreading simultaneously. They are critical area objects, mutectual objects (CMutex), semaphumaphore, and event objects (CEVENT). In these objects, the easiest of the critical area object is the simplest, and its disadvantage is to simultaneously synchronize the thread in the same process. In addition, there is a basic method, referred to as a linearization method, i.e., is done in a thread during the programming process. Thus, since the code in the same thread is always executed in order, it is impossible to change the data at the same time. (Web editing: wind wings)