This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Purpose of pthread_cleanup_push_defer_np()


On Wed, Jul 10, 2013 at 01:17:26PM +0200, Sebastian Huber wrote:
> Hello,
> 
> what is the purpose of pthread_cleanup_push_defer_np()?  From the man page I have:
> 
> http://man7.org/linux/man-pages/man3/pthread_cleanup_pop_restore_np.3.html
> 
>        This sequence of calls:
> 
>            pthread_cleanup_push_defer_np(routine, arg);
>            pthread_cleanup_pop_restore_np(execute);
> 
>        is equivalent to (but shorter and more efficient than):
> 
>            int oldtype;
> 
>            pthread_cleanup_push(routine, arg);
>            pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
>            ...
>            pthread_setcanceltype(oldtype, NULL);
>            pthread_cleanup_pop(execute);
> 
> Why is the cancel type set to PTHREAD_CANCEL_DEFERRED after the
> pthread_cleanup_push()?  In case the thread gets canceled
> asynchronously between the pthread_cleanup_push() and
> pthread_setcanceltype() the routine is invoked.  This is problematic
> in case the routine requires some conditions set by the ... body.
> Here is an example:

Indeed, there is absolutely no way this can be used correctly. If the
change in cancellation type happened outside of the cleanup context
rather than inside it, though, then it could be usable. However any
use of asynchronous cancellation except in pure computational code is
almost surely a bug, so I'm not sure what the practical usage case
would be.

Rich


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]