I try to add a section to my code which is able to unlock the mutex in a case of cancellation. This may happen and would cause a deadlock. Therefore I tried to add pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi);
but this line cause a syntax error from the line where I add it till the end of the code file. If I comment the code line the program will compile without any error.
What I'm doing wrong?
void cleanup_unlock_mutex(void *p){
pthread_mutex_unlock(p);
}
....... }else{
for(unsigned count=0; count <= number_of_requests; count++){
pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi);
pthread_mutex_lock(&mutex_ftdi);
process_requests(count, numberofthread);
pthread_mutex_unlock(&mutex_ftdi);
}
} // compiler error: error: expected ‘while’ before ‘}’ token
..........
All other functions in the file get the warning: ISO C forbids nested functions [-pedantic].
You must call
pthread_cleanup_push()
andpthread_cleanup_pop()
in matching pairs, and your snippet doesn't have thepthread_cleanup_pop()
call.The docs at http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cleanup_pop.html explain why:
To make this concrete, one possible implementation, taken from glibc's
nptl/sysdeps/pthread/pthread.h
: