</>EmbeddedInterview

When should you call pthread_exit() instead of exit()?

MediumLinux System Programming

Short answer: exit() ends the whole process: all threads stop, atexit() handlers run, stdio buffers are flushed. pthread_exit() ends only the calling thread, letting the other threads keep running. Call pthread_exit() when a thread is done but the process should continue, and in particular in main() if you want the worker threads to finish their work instead of being killed when main returns.

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

static void *worker(void *arg)
{
    sleep(1);
    printf("worker %ld done\n", (long)arg);
    return NULL;                 /* same as pthread_exit(NULL) in a thread function */
}

int main(void)
{
    pthread_t t[3];
    for (long i = 0; i < 3; i++)
        pthread_create(&t[i], NULL, worker, (void *)i);

    pthread_exit(NULL);          /* main thread ends; workers keep running;
                                    the process exits when the last thread ends */
    /* "return 0;" here instead would call exit() and kill the workers immediately */
}

Key points

  • Returning from main() is the same as calling exit(). Any threads still running are terminated, possibly in the middle of their work.
  • Returning from a thread's start function is the same as pthread_exit() with that return value.
  • The value passed to pthread_exit(retval) can be collected by another thread with pthread_join(). Do not return a pointer to the thread's own stack variables; they no longer exist.
  • pthread_exit() runs thread cleanup handlers (pthread_cleanup_push) and thread-specific data destructors; it does not release mutexes the thread holds or close files (files belong to the process).
  • Any thread calling exit() ends the whole process; a library should never call exit() on an error in a worker thread.
  • _exit() / _Exit() end the process immediately without running atexit handlers or flushing stdio; use them in a child after a failed exec().

Better practice than pthread_exit in main: pthread_join() each worker before returning from main. Then you know when work is complete and can collect results and report errors.