Skip to main content


Showing posts from October, 2013

Programming: Windows Threading Vs Linux Threading (Part 6)

A thread cleanup handler in Linux and Windows thread cancellation: A cleanup handler is a measure, used to deallocate a resource only if the thread exits or canceled. To register a cleanup handler, need to call pthread_cleanup_push() and pass a pointer to cleanup function and the void * argument. The pthread_cleanup_pop() function removes the routine at the top of the calling thread's cancellation cleanup stack and optionally invokes it (if execute is non-zero). The following sample code shows how a dynamically allocated buffer from a thread can be deallocated on canceling the thread in Linux. #include < pthread.h > #include < stdio.h > #include < string.h > #include < unistd.h >  char *pgArray = 0; /* Buffer allocation */ char * allocate_array (size_t size) {     return new char[size]; } void release_array (void *pArg) {     if(pgArray)     {         printf("release_array() called..\n");         delete []pgArray;     }