Search code examples
cmultithreadingconcurrency

Usage of pthread_cleanup_push and pthread_cleanup_pop


I am still new to multithreading programming, I don't quite understand why pthread_cleanup_push and pthread_cleanup_pop are only used when a thread is getting called, whereas calling pthread_exit() does not require cleanup. Why?


Solution

  • The purpose of thread cancellation clean-up handlers, which is what pthread_cleanup_push/pthread_cleanup_pop manage, is to perform cleanup of user defined data and structures used by the thread when it is cancelled. Since cancellation is usually an abnormal termination method, you can register handlers to "clean things up" when a thread is cancelled, because the thread will not be able to do so itself before being terminated.

    For example, say you have a mutex lock shared between two threds A and B. Thread A acquires the lock, and then gets canceled before having a chance to release it. Thread B then tries to acquire the lock, but gets stuck indefinitely because the lock was never (and never will) be released by A. To avoid this, using a thread cancellation clean-up handler, you can make sure that when A is cancelled, the mutex lock is released.

    Under normal circumstances such as return from a thread or pthread_exit(), it is assumed that such a clean-up process is not needed, as it's the thread itself that decides when to exit, and has full control of what to do before exiting, so any clean-up can be performed easily.

    NOTE though that calls to pthread_exit() will still cause the cleanup handlers to run. This is because pthread_exit() could be called by other functions used by the thread that you may or may not have control over (I would say it's an uncommon case, but still).