227 people read comment(0) Collection report

Pthread_cleanup_push void (void (*routine) () (void*), *arg void ();

Void pthread_cleanup_pop (int execute); / / int parameter here, 0 is the implementation of the contents of push, non implementation of the 0.

Prototype is very simple, with the function of atexit () almost, but one is a thread is a process.
What you want to do when the thread exits in the push/pop.

There are several points to pay attention to:
1, push and pop must be in pairs, in fact, push contains "{" and pop contains "}", a little less.
2, push can have more than one, the same pop to the corresponding number, follow the "advanced" principle".

Push functions may be implemented in the following three timing:
1, the display of the call pthread_exit ();
2, the cancel point thread is cancel.
3, pthread_cleanup_pop () parameters are not 0.

All of the above actions are limited to the code that is covered by push/pop.

In front of the two is better understood, the key is pthread cleanup pop parameter problem, in fact, int that is because C is not bool, this parameter only 0 and non-zero difference, the pthread cleanup pop, parameter is 5 and 10 are the same, is non-zero.

We often see this code:
Child void (*t void)
Pthread_cleanup_push (pthread_mutex_unlock, &mutex);
Pthread_mutex_lock (&mutex);
... in...
Pthread_mutex_unlock (&mutex);
Pthread_cleanup_pop (0);

Why pthread_cleanup_pop is 0, he simply does not implement the function push in the function pointer to point to the function, yes, is not executed, really want to execute on the trouble.

So why do you have to write it?

That's because push and pop must be in pairs, not writing is a syntax error.

Write so mainly in order to ensure a mutex can be unlock because, between the pthread mutex lock and pthread mutex unlock may anything can happen. For instance, the existence of N cancel points cause the thread to be main or other threads to cancel, cancel action is will not release the mutex, this lock to death.

By pthread_cleanup_push to add a function pthread_mutex_unlock, referring to the timing of the above instructions, when the thread is cancel, you can release the lock to clean up the action. If the thread executing normally, until run to pthread cleanup pop, then the lock has been in the intermediate code pthread mutex unlock released, this time if pthread cleanup pop parameter is not zero, released again, wrong here, the release of the two.

So, pthread_cleanup_pop (0) is a must, because, first of all to appear in pairs, and secondly, we do not want him to really implement this release two times.

Same reason:
*exit1 void (*t void)
Printf ("exit1\n");
*child void (*t void)
Pthread_cleanup_push (exit1, NULL);
Pthread_exit (NULL);
Pthread_cleanup_pop (0);

Exit1 function is in pthread_exit (NULL) when the implementation of the pthread_cleanup_pop parameter is not 0 no relationship, because it does not perform here.

And replaced it like this:
Pthread_cleanup_push (exit1, NULL);
Pthread_cleanup_pop (0);
Pthread_exit (NULL);

The exit1 will not be executed because the parameter of pop is 0. If the parameter of the pop is changed to 1, the exit1 will be executed two times. NO, because pthread_exit is outside of block push/pop, he won't trigger exit1.

Pthread_cleanup_push (exit1, NULL);
Pthread_cleanup_push (exit2, NULL);
Pthread_cleanup_pop (0);
Pthread_cleanup_pop (1);

Those 0 and 1 were in control of who? Matching principle, from the outside to the inside of a pair of pull out on it, obviously, 0 of the control is exit2.
Pthread_cleanup_push (void () (*fun) (Arg void*), Arg (void*);
Pthread_cleanup_pop (int);
Pay attention to the use of pthread_cleanup_pop (int)
If pthread_cleanup_push and pthread_cleanup_pop contain the code to perform the normal, then in the
Pthread_cleanup_pop this sentence, if the parameter is 0, will not perform push into the function, otherwise the implementation.
step on
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit4853 times
    • Integral:One hundred and eighty-eight
    • Grade
    • Rank:Thousands of miles away
    • Original12
    • Reproduced:21
    • Translation:0
    • Comments:0
    Classification of articles