12.3.4 终止线程

12.3.4 终止线程

一个线程是以下列方式之一来终止的:

  • 当顶层的线程例程返回时,线程会隐式地终止。
  • 通过调用 pthread_exit 函数,线程会显式地终止。如果主线程调用 pthread_exit,它会等待所有其他对等线程终止,然后再终止主线程和整个进程,返回值为 thread_return
#include <pthread.h>

void pthread_exit(void *thread_return);

从不返回。

  • 某个对等线程调用 Linux 的 exit 函数,该函数终止进程以及所有与该进程相关的线程。
  • 另一个对等线程通过以当前线程 ID 作为参数调用 pthread_cancel 函数来终止当前线程。
#include <pthread.h>

int pthread_cancel(pthread_t tid);

若成功则返回 0,若出错则为非零。