Termination

The exit(3) library API causes the calling process—along with all of its threads – to terminate. If you would like a single thread to terminate, have it invoke the pthread_exit(3) API instead:

#include <pthread.h> void pthread_exit(void *retval);

This parameter specifies the exit status of the calling thread; for the time being, we ignore it and just pass NULL (we shall examine using this parameter shortly).

So, back to our racy app (ch14/pthreads1.c); let's make a second, better version (ch14/pthreads2.c). The problem, really, with our first version was the race—the main thread calls exit(3), causing the entire process to die, probably before the worker threads got a chance to complete their work. So, let's fix this by having ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.