Previous post
The impact of Process Termination on the structure of the C ++ object. Today, let's talk about the impact of threads on the analysis structure of C ++ objects.
Since the C ++ 03 standard does not contain the thread concept, C ++
0x has not been officially released. Therefore, we have to discuss the thread based on the specific operating system platform. For the built-in thread APIs of the operating system, the popular style is the thread APIs provided by the Windows platform and pthread APIs on the POSIX platform. However, the differences between the two thread APIs are too large to be discussed together. I had to split the "Thread" post. Today I will talk about the Win32 thread API.
In addition, some third-party cross-platform thread libraries (such as Ace and boost) should be used for cross-platform development, I plan to put it in "C ++ portability and cross-platform development"
.
★Two APIs
:OS API
VSCRT API
In this example, we should first introduce several dead-end methods of the thread, but considering that many windows programmers often confuse thread APIs, we cannot figure out which method to use. So let's talk about the two sets of thread APIs first.
First, the Windows operating system provides the thread creation function.Createthread
And destroy FunctionsExitthread
. TheCreatethread
Used to create a thread,Exitthread
It is used to launch a thread (that is, suicide) within the thread function ).
Secondly, in the C Runtime Library (CRT) that comes with Visual C ++, four other API functions are included:_ Beginthread
,_ Endthread
,_ Beginthreadex
,_ Endthreadex
. The_ Beginthread
And_ Beginthreadex
Used to create threads (they callCreatethread
),_ Endthread
And_ Endthreadex
Used for suicide (they are called internallyExitthread
).
Some people saw this and got confused. They thought, "Why do you want to make so many things to fool people? YesCreatethread
AndExitthread
That's enough !" In fact, you don't know about it.
As an API function provided by the operating system, OS APIs are designed to be language-independent. They can be called not only by C ++, but also by other development languages such as VB, Python, and Delphi. So they won't (and cannot) help you deal with things related to specific programming languages.
Although the crt api still needs to call the OS API to complete core functions, the CRT API has helped us do some trivial but important work without knowing it. (If you want to get a glimpse of what the crt api has done internally, you can read the Windows core programming, a classic Win32 programming masterpiece .)6.7
Chapter, which is quite detailed)
Having paid so much saliva, the students should remember that, in the future, they will develop multi-threaded programs on the Windows platform,Never
Directly use these two thread APIs (that isCreatethread
AndExitthread
). Otherwise, the consequences will be borne by you.
In addition, by the way. In addition to the CRT Library mentioned above. Other Windows C ++ libraries may also provide thread startup functions (such as afxbeginthread of MFC). These functions are also packaged for OS APIs, therefore, it is safe to use.
★Three methods
After talking about the two sets of APIS, let's start to discuss several dead methods of the thread. Like a process, there are also three dead-end methods. For details, see:
1. Natural Death
Generally, each thread corresponds to a function (hereinafter referred to as a "thread function "). A thread function is the main body of a thread. The so-called "natural death" means thatReturn
Statement.
2. Suicide
The so-called "suicide" means that the current thread calls an APIYourself
Stop. I have already said bad things about the OS API. Students should understandNo
Use them again. Can we use the crt api to commit suicide? Please refer to the relevant documentation on msdn
. As mentioned above, if you use_ Endthread
And_ Endthreadex
, Will cause the destructorNot
Call.
3. It kills
The so-called "It kill" means that other threads call an APIForcible
Stop. For Windows platforms, it is relatively simple to implement "it kill ".Ternimatethread
You can simply kill it (it is also the most brutal ).
★Class Object Structure
According to the previous post
Of
Style: class objects are divided into three types: local non-static objects, local static objects, and non-local objects. Because non-local objects are created before the main, and are parsed when the process is dead, they are not very closely related to the thread at the moment. The remaining two local objects are affected when the host thread (the so-called host thread is the thread that creates the local object) dies? See the following table:
-------------------------
Local non-static object local static object
Natural death
Suicide cannot
It cannot kill
-------------------------
From the above results, we can see that the thread dead on Windows is still the safest, similar to the process dead. Therefore, when developing on Windows, students should try to avoid suicide and killing them.
★The death of the main thread
The so-called "main thread" is the first thread created by the operating system for the process by default when the process is started. In other wordsMain
The function is regarded as a thread function of the main thread.
The death of the main thread is exquisite. As we have already discussed the disadvantages of unnatural death, we will only discuss the natural death of the main thread. When the main thread naturally dies (that is, whenReturn
SlaveMain
), Will causeExit
Function called,Exit
The function begins to clear the resources of the current process and prepares for the death of the process. At this time, if there are other living threads, they will also be killed together (the effect is similar to killing it ).
To prevent the above situation, the main thread must be responsible for the final aftermath. Wait until all other threads die before it can die.
On the Windows platform, we can talk about the thread object structure. Next post
Let's talk about object analysis topics related to pthread.
Copyright Notice
All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:
Http://program-think.blogspot.com/2009/03/cxx-object-destroy-with-thread-win32.html