pthread_cleanup_push

Hello!

Anybody!

Explain me, please:

Q: In which thread cleanup routine unlock will be executed in below example ?
Q: if thread_cancel(tid) will be called from another thread – which therad calls cleanup function ?
Q: Has this cleanup function access to thread private data (created by pthread_key_create call) in both cases ?

(the following is example from QNX6.1 Library Reference)

void unlock( void * arg ){
pthread_mutex_unlock( &lock );
}

void * function( void * arg ){
while( 1 ) {
pthread_mutex_lock( &lock );
pthread_cleanup_push( &unlock, NULL );
/* Any of the possible cancellation points could go here. */
pthread_testcancel();
pthread_cleanup_pop( 1 );
}
}

Thanks a lot

Vasilii

vasa <vv40in@mail.ru> wrote:
: Hello!

: Anybody!

: Explain me, please:

: Q: In which thread cleanup routine unlock will be executed in below example ?
: Q: if thread_cancel(tid) will be called from another thread – which therad calls cleanup function ?
: Q: Has this cleanup function access to thread private data (created by pthread_key_create call) in both cases ?

: (the following is example from QNX6.1 Library Reference)

: void unlock( void * arg ){
: pthread_mutex_unlock( &lock );
: }

: void * function( void * arg ){
: while( 1 ) {
: pthread_mutex_lock( &lock );
: pthread_cleanup_push( &unlock, NULL );
: /* Any of the possible cancellation points could go here. */
: pthread_testcancel();
: pthread_cleanup_pop( 1 );
: }
: }


pthread_cleanup_{push,pop} in Neutrino are macros that save the thread cancellation
routines on the stack. The targetted thread will run them when:

  • pthread_exit() is call
  • on a cancellation request
  • for pthread_cleanup_pop() with a non-zero argument.

Since the cancellation routine that the targetted thread calls are on his stack
you should not have a longjmp() or siglongjump() within a pthread_cleanup_push()
and a pthread_cleanup_pop(), nor within the cancellation routines.


alain