首页 > 编程笔记

pthread_exit()函数:终止线程

多线程编程中,线程结束执行的方式有 3 种,分别是:
  1. 线程将指定函数体中的代码执行完后自行结束;
  2. 线程执行过程中,被同一进程中的其它线程(包括主线程)强制终止;
  3. 线程执行过程中,遇到 pthread_exit() 函数结束执行。

注意,默认属性的线程执行结束后并不会立即释放占用的资源,直到整个进程执行结束,所有线程的资源以及整个进程占用的资源才会被操作系统回收。

实现线程资源及时回收的常用方法有两种,一种是修改线程属性,另一种是在另一个线程中调用 pthread_join() 函数,我们会在后续章节中给您详细介绍这两种方法。

线程结束执行的 3 种方式中,第 1 种很容易理解,我们会在《pthread_cancel()函数》一文中介绍第 2 种方式,本文重点讲解 pthread_exit() 函数的功能和用法。

pthread_exit()函数的用法

Linux pthread_exit() 函数声明在<pthread.h>头文件中,语法格式如下所示:
void pthread_exit(void *retval);
retval 是 void* 类型的指针,可以指向任何类型的数据,它指向的数据将作为线程退出时的返回值。如果线程不需要返回任何数据,将 retval 参数置为 NULL 即可。

注意,retval 指针不能指向函数内部的局部数据(比如局部变量)。换句话说,pthread_exit() 函数不能返回一个指向局部数据的指针,否则很可能使程序运行结果出错甚至崩溃。

接下来通过一个样例,给大家演示 pthread_exit() 函数的用法(样例一):
#include <stdio.h>
#include <pthread.h>

//线程要执行的函数,arg 用来接收线程传递过来的数据
void *ThreadFun(void *arg)
{
    //终止线程的执行,将“http://c.biancheng.net”返回
    pthread_exit("http://c.biancheng.net"); //返回的字符串存储在常量区,并非当前线程的私有资源
    printf("*****************");//此语句不会被线程执行
}

int main()
{
    int res;
    //创建一个空指针
    void * thread_result;
    //定义一个表示线程的变量
    pthread_t myThread;

    res = pthread_create(&myThread, NULL, ThreadFun, NULL);
    if (res != 0) {
        printf("线程创建失败");
        return 0;
    }
    //等待 myThread 线程执行完成,并用 thread_result 指针接收该线程的返回值
    res = pthread_join(myThread, &thread_result);
    if (res != 0) {
        printf("等待线程失败");
    }
    printf("%s", (char*)thread_result);
    return 0;
}
假设程序存储在 thread.c 文件中,执行过程如下:

[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe
http://c.biancheng.net

通过执行结果不难看出,myThread 线程并没有执行 ThreadFun() 函数中最后一个 printf() 语句,从侧面验证了 pthread_exit() 函数的功能。此外,我们通过在主线程(main() 函数)调用 pthread_join() 函数,获取到了 myThread 线程返回的数据。有关 pthread_join() 函数的功能和用法,我们会在《获取线程函数返回值》一节中给大家讲解。

pthread_exit() 和 return 的区别

如果想在线程执行结束时返回指定的数据,除了用 pthread_exit() 函数外,还可以使用 return 语句。

修改样例一中的程序,将第 8 行(调用 pthread_exit() )代码替换成如下语句:
return "http://c.biancheng.net";
重新编译、执行此程序,会发现程序的执行结果和之前完全相同。这意味着当线程执行结束时,无论是采用 return 语句还是调用 pthread_exit() 函数,主线程中的 pthread_join() 函数都可以接收到线程的返回值。

那么,return 语句和 pthread_exit() 函数的区别是什么呢?

首先,return 语句和 pthread_exit() 函数的含义不同,return 的含义是返回,它不仅可以用于线程执行的函数,普通函数也可以使用;pthread_exit() 函数的含义是线程退出,它专门用于结束某个线程的执行。

在主线程(main() 函数)中,return 和 pthread_exit() 函数的区别最明显。举个例子:
#include <stdio.h>
#include <pthread.h>

void *ThreadFun(void *arg)
{
    sleep(5);//等待一段时间
    printf("http://c.biancheng.net\n");
}

int main()
{
    int res;
    pthread_t myThread;
     
    res = pthread_create(&myThread, NULL, ThreadFun, NULL);
    if (res != 0) {
        printf("线程创建失败");
        return 0;
    }
    printf("C语言中文网\n");
    return 0;
}
编译、执行此程序,输出结果为:

C语言中文网

通过执行结果可以看到,主线程正常执行结束,myThread 线程并没有输出指定的数据。原因很简单,主线程执行速度很快,主线程最后执行的 return 语句不仅会终止主线程执行,还会终止其它子线程执行。也就是说,myThread 线程还没有执行输出语句就被终止了。

将上面程序中,main() 函数中的return 0;用如下语句替换:
pthread_exit(NULL);
重新编译、执行程序,运行结果为:

C语言中文网
http://c.biancheng.net

对比上面两个执行结果,我们可以得出的结论是:pthread_exit() 函数只会终止当前线程,不会影响进程中其它线程的执行。

此外,pthread_exit() 可以自动调用线程清理程序(本质是一个由 pthread_cleanup_push() 指定的自定义函数),return 则不具备这个能力。总之在实际场景中,如果想终止某个子线程执行,强烈建议大家使用 pthread_exit() 函数。终止主线程时,return 和 pthread_exit() 函数发挥的功能不同,可以根据需要自行选择。

推荐阅读