When the interface is suspended or the task volume is large
Memory error. Pay attention to memory release
Reference others' experiences
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.
When a process is created based on the implementation principle of multithreading, 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 thread with quantum in rotation mode. Each thread hand over the control after using the time slice,
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.
Multithreading
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 using the following solution:
The first method is to lock the printed document and ask the user to edit other documents so that the document will not be modified before the printing ends;
Another method may be more effective: You can copy the document to a temporary file, print the content of the 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 on the front end. For GUI (graphical user interface ),
In addition to developing MDI (Multi-Document Interface) applications, do not use multithreading as much as possible.
Thread Classification
In MFC, threads are divided into two types: worker threads and user interface threads.
If a thread only performs background computing and does not need to interact with users, a working thread can be used;
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 ),
First, we should consider using a working thread. When the background thread needs to process complicated tasks,
Specifically, when the execution process of the background thread changes with the actual situation,
The User Interface thread should be used 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 a thread in a program, the system automatically and dynamically changes the priority of a thread during execution,
This is to ensure that the system is highly responsive to end users. For example, when a user presses 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.
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,
In this way, it is especially important to ensure that data between threads is not broken.
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.