Multithreaded Programming Guide

What the Thread Pool Example Shows

The example illustrates cancellation and unexpected thread termination, which is one of the trickier aspects of programming with threads. A worker thread might exit by calling pthread_exit() from within the task function passed to thr_pool_queue(), rather than just returning from the task function as expected. The thread pool recovers from this by catching the termination in a pthread_cleanup_push() function. The only harm done is that another worker thread must then be created. Worker threads that are actively processing tasks are cancelled in thr_pool_destroy(). A caller of thr_pool_wait() or thr_pool_destroy() may be cancelled by the application while it is waiting. This is also dealt with by using pthread_cleanup_push().

Although the example package is useful as it is, an application might require some features that are missing here, such as: