5. Thread termination method: pthread_cleanup_push/pthread_cleanup_pop ()

Source: Internet
Author: User
Pthread_cleanup_push/pthread_cleanup_pop ()

The following content is as follows 【1. The cancellation point will be further discussed later.

1,In general, POSIXThere are two cases of thread termination:Normal or abnormal termination. The thread actively calls pthread_exit ()Or from the thread functionReturnWill make the thread exit normally, this is the foreseeable way to exit;Abnormal Termination is caused by thread intervention by other threads or exit due to running errors (such as access to invalid addresses). This exit mode is unpredictable.

2,Cleanup upon thread termination

Whether it is foreseeable thread termination or abnormal termination, there will be a problem of resource release, without considering the exit due to a running error, how to ensure that the resources occupied by the thread can be released smoothly when the thread is terminated, especially the resource lock, is a problem that must be considered.

The most common situation is the use of the resource exclusive lock: the thread wants to accessCritical resourcesThe lock is added to the thread, but the access is canceled by the outside world. If the thread is inResponds to the canceled status and responds asynchronously.Or exists in the running path before the exclusive lock is opened.Cancel pointThe critical resource is always locked and cannot be released. Undo operations are unpredictable, so a mechanism is required to simplify programming for resource release.

In POSIXThreadAPIProvidesPthread_cleanup_push ()/ Pthread_cleanup_pop ()Function pairs are used to automatically release resources.-SlavePthread_cleanup_push ()Call PointPthread_cleanup_pop ()BetweenProgramThe termination action (Including calling pthread_exit ()And cancel point termination) Will execute pthread_cleanup_push ()The specified cleanup function.

APIDefinition:

Void pthread_cleanup_push (void (* routine) (void *), void * Arg)

Void pthread_cleanup_pop (INT execute)

Pthread_cleanup_push ()/pthread_cleanup_pop ()Use the stack structure management that comes first and then goes out,Void routine (void * Arg)Function callingPthread_cleanup_push ()Push in to clear function Stack multiple timesPthread_cleanup_push ()Will form a function chain in clearing the function stack.;SlavePthread_cleanup_pushCall PointPthread_cleanup_popThe termination action (including the callPthread_exit ()And termination of exceptions, not includingReturn) Will be executedPthread_cleanup_push ()The specified cleanup function.

In executionTheFunction links are popped up in reverse order of pressure stacks. ExecuteThe parameter indicates thatPthread_cleanup_pop ()Whether to execute this function while the cleanup function is popped up.0Indicates not to execute, not0For execution;This parameter does not affect the execution of the cleanup function when an exception is terminated..

Pthread_cleanup_push ()/pthread_cleanup_pop ()It is implemented in a macro way. This isPthread. hMacro definition in:

# Define pthread_cleanup_push (routine, ARG )\

{

Struct _ pthread_cleanup_buffer _ buffer ;\

_ Pthread_cleanup_push (& _ buffer, (routine), (ARG ));

 

# Define pthread_cleanup_pop (execute )\

_ Pthread_cleanup_pop (& _ buffer, (execute ));\

}

Visible, pthread_cleanup_push ()With"{", AndPthread_cleanup_pop ()With"}"Therefore, these two functions must appear in pairs and must be at the same level of the programCodeCan be compiled.

In the following example, when the thread is in "Do some work"Will be calledPthread_mutex_unlock (MUT)To complete the unlock action.

Pthread_cleanup_push (pthread_mutex_unlock, (void *) & MUT );

Pthread_mutex_lock (& MUT );

/* Do some work */

Pthread_mutex_unlock (& MUT );

Pthread_cleanup_pop (0 );

【1]Http://wenku.baidu.com/view/fd4a162e0066f5335a812191.html

The following content is as follows 【1. The cancellation point will be further discussed later.

1,In general, POSIXThere are two cases of thread termination:Normal or abnormal termination. The thread actively calls pthread_exit ()Or from the thread functionReturnWill make the thread exit normally, this is the foreseeable way to exit;Abnormal Termination is caused by thread intervention by other threads or exit due to running errors (such as access to invalid addresses). This exit mode is unpredictable.

2,Cleanup upon thread termination

Whether it is foreseeable thread termination or abnormal termination, there will be a problem of resource release, without considering the exit due to a running error, how to ensure that the resources occupied by the thread can be released smoothly when the thread is terminated, especially the resource lock, is a problem that must be considered.

the most common case is the use of the exclusive resource lock: the thread adds a lock to the critical resource to access the resource, however, when the access is canceled, if the thread is in the canceled status, asynchronous response , or there is a cancel point >, the critical resource is always locked and cannot be released. Undo operations are unpredictable, so a mechanism is required to simplify programming for resource release.

In POSIXThreadAPIProvidesPthread_cleanup_push ()/ Pthread_cleanup_pop ()Function pairs are used to automatically release resources.-SlavePthread_cleanup_push ()Call PointPthread_cleanup_pop ()The termination action (Including calling pthread_exit ()And cancel point termination) Will execute pthread_cleanup_push ()The specified cleanup function.

APIDefinition:

Void pthread_cleanup_push (void (* routine) (void *), void * Arg)

Void pthread_cleanup_pop (INT execute)

Pthread_cleanup_push ()/pthread_cleanup_pop ()Use the stack structure management that comes first and then goes out,Void routine (void * Arg)Function callingPthread_cleanup_push ()Push in to clear function Stack multiple timesPthread_cleanup_push ()Will form a function chain in clearing the function stack.;SlavePthread_cleanup_pushCall PointPthread_cleanup_popThe termination action (including the callPthread_exit ()And termination of exceptions, not includingReturn) Will be executedPthread_cleanup_push ()The specified cleanup function.

In executionTheFunction links are popped up in reverse order of pressure stacks. ExecuteThe parameter indicates thatPthread_cleanup_pop ()Whether to execute this function while the cleanup function is popped up.0Indicates not to execute, not0For execution;This parameter does not affect the execution of the cleanup function when an exception is terminated..

Pthread_cleanup_push ()/pthread_cleanup_pop ()It is implemented in a macro way. This isPthread. hMacro definition in:

# Define pthread_cleanup_push (routine, ARG )\

{

Struct _ pthread_cleanup_buffer _ buffer ;\

_ Pthread_cleanup_push (& _ buffer, (routine), (ARG ));

 

# Define pthread_cleanup_pop (execute )\

_ Pthread_cleanup_pop (& _ buffer, (execute ));\

}

Visible, pthread_cleanup_push ()With"{", AndPthread_cleanup_pop ()With"}"Therefore, these two functions must appear in pairs and must be located in the code segment at the same level of the program before being compiled.

In the following example, when the thread is in "Do some work"Will be calledPthread_mutex_unlock (MUT)To complete the unlock action.

Pthread_cleanup_push (pthread_mutex_unlock, (void *) & MUT );

Pthread_mutex_lock (& MUT );

/* Do some work */

Pthread_mutex_unlock (& MUT );

Pthread_cleanup_pop (0 );

【1]Http://wenku.baidu.com/view/fd4a162e0066f5335a812191.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.