[Z] Two main thread features

Source: Internet
Author: User

Link to this article:Http://www.titilima.cn/show-544-1.html
First look at a piece of citation, the source is not clear, but it must be a paper publication.

 

>ProgramThe thread that is executed after startup is called the primary thread. The main thread has two
> Features. First, itRequiredResponsible for the main message circulation in the graphic user interface (GUI) Program
> Ring. Second, the end of this thread (either becauseBecause exitthread () is called ()) Yes
> So that all threads in the program are forced to end and the program ends. Other threads have no chance
> Perform cleanup.

When I read a book, I often only know a rough sentence, and a sentence like this is basically something I ignore. However, at an accidental opportunity, I noticed this sentence and had the following comments.

Conclusion: The two features of the main thread mentioned in the sentence are basically unreliable.

> First, itRequiredResponsible for the main message loop in the graphic user interface program.
The reason why this statement is unreliable is that it excessively limits the functions of the main thread. In my impression, in versions earlier than 4.0, the main thread is a scheduler used to schedule other worker threads, the UI thread is also one of these worker threads. In other words, the main message loop may not be in the main thread.

> Second, the end of this thread (whether it is returned orBecause exitthread () is called ()All threads in the program are forced to end, and the program ends.
This is a half-to-half error, because if exitthread is called in the main thread, other threads will not exit, so the program will not end. Consider the followingCode:

C ++ code
  1. # Include <windows. h>
  2. DWORDWinapi threadproc (PvoidParam)
  3. {
  4. For(;;)
  5. {
  6. Sleep (1000 );
  7. }
  8. Return0;
  9. }
  10. IntMain (Void)
  11. {
  12. HandleHthread = createthread (null, 0, threadproc, null, 0, null );
  13. Sleep (1000 );
  14. Exitthread (0 );
  15. Return0;
  16. }

After the program runs, the main thread is terminated by exitthread, but the threadproc thread runs continuously. The following is a good explanation of this question in the Windows programming notes.

---------- The legendary separation line ----------

So, let's explore the normal process of Process Termination. To avoid unnecessary code interference, I chose an empty skeleton program.

C ++ code
  1. # Include <windows. h>
  2. IntWinapi winmain (
  3. HinstanceHinstance,
  4. HinstanceHprevinstance,
  5. LpstrLpcmdline,
  6. IntNshowcmd)
  7. {
  8. Return0;
  9. }

As you can see, this program is really worthy of the title of "skeleton"-it is really a shelf except for bones.
On the surface, this winmain is the entry of the program. When it returns (return 0;), our process ends. Of course, the fact is certainly not as simple as it seems, because the compiler is very happy to secretly do good things, and there is still a habit of refusing to leave a name when doing good things.
Now let's compile this code and use windbg to debug this program. We do not set any breakpoints, so the program runs until the end. At this time, the call stack information of the program will look like the following:

Windbg output
  1. 0: 000> K
  2. Childebp retaddr
  3. 0012fdc4 7c92e89a NTDLL! Kifastsystemcallret
  4. 0012fdc8 7c81ca3e NTDLL! Zwterminateprocess + 0xc
  5. 0012fec4 7c81ca96 Kernel32! _ Exitprocess + 0x62
  6. 0012fed8 004012a1 Kernel32! Exitprocess + 0x14
  7. 0012fee4 0040148d skeleton! _ Crtexitprocess + 0x17
  8. 0012ff28 004014b7 skeleton! Doexit + 0x113
  9. 0012ff3c 0040114f skeleton! Exit + 0x11
  10. 0012ffc0 7c816ff7 skeleton! _ Tmaincrtstartup + 0x121
  11. 0012fff0 00000000 Kernel32! Baseprocessstart + 0x23

The truth is finally revealed in the world. Obviously, the compiler hides the exitprocess API of kernel32.dll behind winmain, so that our skeleton.exe process can exit. If you are interested in how to encapsulate exitprocess details, you can take a deeper look at the C RuntimeSource codeCrt0.c file in.

---------- The legendary separation line ----------

Obviously, the execution of exitthread causes the thread to end, that is, the exitprocess In the CRT is skipped directly, so that the process cannot end.

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.