How does a C ++ object die? POSIX thread

Source: Internet
Author: User

Previous post
After talking about the death of the C ++ object related to the thread in the Win32 environment, let's talk about the POSIX thread library pthread today. If you do not know much about pthread, check wikipedia first.
.

Three methods

To put it bluntly, we will first introduce three methods of death.
1. Natural Death
Previous post
We have already introduced the natural death of Win32 threads. The natural death of pthread is similar to that of it. It is also the corresponding thread function returned through return.
2. Suicide
For "suicide", POSIX usesPthread_exit
Function. This method is much easier than Win32's suicide, saving a lot of saliva.
3. It kills
Although pthread's suicide is simple, it's complicated to kill it. So I moved the saliva here, focusing on how it was killed.
Mainly used in the pthread LibraryPthread_cancel
Kill the thread. UsePthread_cancel
There are two cases for canceling a thread:Asynchronous Cancellation
(Pthread_cancel_asynchronous) andDelayed cancellation
(Pthread_cancel_deferred ).
Asynchronous cancellation is very crude. No matter how many threads are killed, the thread is killed directly. The delay cancellation is gentle. The canceled thread continues to run until it encounters a "cancellation point. Because this post is not
Pthread's entry-level literacy post. For details about the "cancellation point" and other details about thread cancellation, see the pthread API manual.
Some may ask,Pthread_kill
Isn't it used to "Kill it? ActuallyPthread_kill
It only sends signals to the specified thread. Well, I didn't know which guy started.Pthread_kill
This function name has misled many people. In additionPthread_kill
Be careful if the corresponding thread does notProcessing
The received signal may affectProcess
, May cause Process Termination (equivalent to process suicide ).

Class Object Structure

Previous post
I have analyzed the C ++ objects related to threads, that is, two local objects. Check whether the two objects are correctly parsed based on different dead methods.
-------------------------
Local non-static object local static object
Natural death
Suicide may not be possible
It cannot kill (Delay)
It cannot be killed (asynchronously)
-------------------------
For the "not necessarily" in the above comparison table, it can be analyzed on the Linux platform (rhel3, GCC 3.2.3), but not in cygwin (Windows2003, GCC 3.4.4, therefore, we have to think that it is "not necessarily an analysis structure ".
Because the implementation of pthread on different operating systems may be different, in some environments, the performance of suicide and its killing may be different from the above. If you find that your actual situation does not match the above, you are welcome to comment or email me, I will add to this article

From the table above, it is the least secure to asynchronously kill, and natural death is the safest. As for suicide and delay, it depends on the specific environment.

The death of the main thread

What is the main thread? The previous post
I have already introduced it. I don't have to worry too much about it. In the POSIX system, the natural death of the main thread also causesExit
Is called, causing other threads to be killed in a brutal manner (similar to in Windows ). You can usePthread_exit
To end the main thread and let other threads continue running. However, thread suicide also occurs in some environments.Insecure
It is recommended that the main thread exit safely.

In the POSIX system, thread-related object structure issues can be discussed here.


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-posix.html

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.