<pthread.h>头文件中,提供有一个和 return 关键字相同功能的 pthread_exit() 函数。和之前不同,pthread_exit() 函数只适用于线程函数,而不能用于普通函数。void pthread_exit(void *retval);
retval 是void*类型的指针,可以指向任何类型的数据,它指向的数据将作为线程退出时的返回值。如果线程不需要返回任何数据,将 retval 参数置为NULL即可。
通过一个样例,给大家演示 pthread_exit() 函数的用法(样例一):注意,retval 指针不能指向函数内部的局部数据(比如局部变量)。换句话说,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
有关 pthread_join() 函数的功能和用法,我们会在《获取线程函数的返回值》一节中给大家讲解。
return "http://c.biancheng.net";重新编译、执行此程序,会发现程序的执行结果和之前完全相同。这意味着当线程执行结束时,无论是采用 return 语句还是调用 pthread_exit() 函数,主线程中的 pthread_join() 函数都可以接收到线程的返回值。
#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 线程还没有执行输出语句就被终止了。return 0;用如下语句替换:
pthread_exit(NULL);重新编译、执行程序,运行结果为:
C语言中文网
http://c.biancheng.net
总之,如果实际场景中想终止某个子线程,强烈建议大家使用 pthread_exit() 函数。终止主线程时,return 和 pthread_exit() 函数发挥的功能不同,可以根据需要自行选择。此外,pthread_exit() 函数还会自动调用线程清理程序(本质是一个由 pthread_cleanup_push() 指定的自定义函数),而 return 不具备这个能力。
<pthread.h>头文件中,语法格式如下:
int pthread_cancel(pthread_t thread);
参数 thread 用于接收 Cancel 信号的目标线程。<errno.h>头文件中,该宏的值为整数 3)。
对于接收 Cancel 信号后结束执行的目标线程,等同于该线程自己执行如下语句:注意,pthread_cancel() 函数的功能仅仅是向目标线程发送 Cancel 信号,至于目标线程是否接收该信号,何时响应该信号,全由目标线程决定。我们会在《终止线程执行,千万别踩这个坑!》一节给您做详细讲解。
pthread_exit(PTHREAD_CANCELED);也就是说,当一个线程被强制终止执行时,它会返回
PTHREAD_CANCELED这个宏(定义在<pthread.h>头文件中)。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h> // sleep() 函数
//线程执行的函数
void * thread_Fun(void * arg) {
printf("新建线程开始执行\n");
sleep(10);
}
int main()
{
pthread_t myThread;
void * mess;
int value;
int res;
//创建 myThread 线程
res = pthread_create(&myThread, NULL, thread_Fun, NULL);
if (res != 0) {
printf("线程创建失败\n");
return 0;
}
sleep(1);
//向 myThread 线程发送 Cancel 信号
res = pthread_cancel(myThread);
if (res != 0) {
printf("终止 myThread 线程失败\n");
return 0;
}
//获取已终止线程的返回值
res = pthread_join(myThread, &mess);
if (res != 0) {
printf("等待线程失败\n");
return 0;
}
//如果线程被强制终止,其返回值为 PTHREAD_CANCELED
if (mess == PTHREAD_CANCELED) {
printf("myThread 线程被强制终止\n");
}
else {
printf("error\n");
}
return 0;
}
假设程序编写在 thread.c 文件中,执行过程为:
[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe
新建线程开始执行
myThread 线程被强制终止
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有