【多线程编程学习笔记6】终止线程执行,千万别踩这个坑!

申明:本学习笔记是在该教程的基础上结合自己的学习情况进行的总结,不是原创,想要看原版的请看C语言中文网的多线程编程(C语言+Linux),该网站有很多好的编程学习教程,尤其是关于C语言的。

在《终止线程执行(3种方法)》一节中,我们对 pthread_cancel() 函数的功能和用法做了详细的介绍。总的来说,通过调用 pthread_cancel() 函数,一个线程可以向同进程内的另一个线程发送“终止执行”的信号(Cancel 信号),使目标线程结束执行。

实际使用 pthread_cancel() 函数时,很多读者会发现“Cancel 信号成功发送,但目标线程并未立即终止执行”等类似的问题举个例子,在 Linux 环境中执行如下程序:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>   //调用 sleep() 函数
void * thread_Fun(void * arg) {
    printf("新建线程开始执行\n");
    //插入无限循环的代码,测试 pthread_cancel()函数的有效性
    while(1);
}
int main()
{
    pthread_t myThread;
    void * mess;
    int value;
    int res;
    res = pthread_create(&myThread, NULL, thread_Fun, NULL);
    if (res != 0) {
        printf("线程创建失败\n");
        return 0;
    }
    sleep(1);
    //令 myThread 线程终止执行
    res = pthread_cancel(myThread);
    if (res != 0) {
        printf("终止 myThread 线程失败\n");
        return 0;
    }
    printf("等待 myThread 线程执行结束:\n");
    res = pthread_join(myThread, &mess);
    if (res != 0) {
        printf("等待线程失败\n");
        return 0;
    }
    if (mess == PTHREAD_CANCELED) {
        printf("myThread 线程被强制终止\n");
    }
    else {
        printf("error\n");
    }
    return 0;
}

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210711094517042.png

程序中,主线程( main() 函数)试图调用 pthread_cancel() 函数终止 myThread 线程执行。从运行结果不难发现,pthread_cancel() 函数成功发送了 Cancel 信号,但目标线程仍在执行。

也就是说,接收到 Cancel 信号的目标线程并没有立即处理该信号,或者说目标线程根本没有理会此信号。解决类似的问题,我们就需要搞清楚目标线程对 Cancel 信号的处理机制。

根据上节的内容,pthread_join会阻塞调用它的线程,因此程序在执行完printf(“等待 myThread 线程执行结束:\n”);后就会一直阻塞在res = pthread_join(myThread, &mess);等待目标线程myThread执行完毕

线程对Cancel信号的处理

对于默认属性的线程,当有线程借助 pthread_cancel() 函数向它发送 Cancel 信号时,它并不会立即结束执行,而是选择在一个适当的时机结束执行。

所谓适当的时机,POSIX 标准中规定,当线程执行一些特殊的函数时,会响应 Cancel 信号并终止执行,比如常见的 pthread_join()、pthread_testcancel()、sleep()、system() 等,POSIX 标准称此类函数为“cancellation points”(中文可译为“取消点”)。

POSIX 标准中明确列举了所有可以作为取消点的函数,这里不再一一罗列,感兴趣的读者可以自行查阅 POSIX 标准手册。

此外,<pthread.h> 头文件还提供有 pthread_setcancelstate() 和 pthread_setcanceltype() 这两个函数,我们可以手动修改目标线程处理 Cancel 信号的方式。

1、pthread_setcancelstate()函数

借助 pthread_setcancelstate() 函数,我们可以令目标线程处理 Cancal 信号,也可以令目标线程不理会其它线程发来的 Cancel 信号。

pthread_setcancelstate() 函数的语法格式如下:

int pthread_setcancelstate( int state , int * oldstate ); 
  1. state 参数有两个可选值,分别是:
  • PTHREAD_CANCEL_ENABLE(默认值):当前线程会处理其它线程发送的 Cancel 信号;
  • PTHREAD_CANCEL_DISABLE:当前线程不理会其它线程发送的 Cancel 信号,直到线程状态重新调整为 PTHREAD_CANCEL_ENABLE 后,才处理接收到的 Cancel 信号。
  1. oldtate 参数用于接收线程先前所遵循的 state 值,通常用于对线程进行重置。如果不需要接收此参数的值,置为 NULL 即可。

pthread_setcancelstate() 函数执行成功时,返回数字 0,反之返回非零数。

2、pthread_setcanceltype()函数

当线程会对 Cancel 信号进行处理时,我们可以借助 pthread_setcanceltype() 函数设置线程响应 Cancel 信号的时机。

pthread_setcanceltype() 函数的语法格式如下:

int pthread_setcanceltype( int type , int * oldtype );
  1. type 参数有两个可选值,分别是:
  • PTHREAD_CANCEL_DEFERRED(默认值):当线程执行到某个可作为取消点的函数时终止执行;
  • PTHREAD_CANCEL_ASYNCHRONOUS:线程接收到 Cancel 信号后立即结束执行。
  1. oldtype 参数用于接收线程先前所遵循的 type 值,如果不需要接收该值,置为 NULL 即可。

pthread_setcanceltype() 函数执行成功时,返回数字 0,反之返回非零数。

接下来通过一个实例给大家演示以上两个函数的功能和用法:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>   //调用 sleep() 函数

void * thread_Fun(void * arg) {
    printf("新建线程开始执行\n");
    int res;
    //设置线程为可取消状态
    res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (res != 0) {
        printf("修改线程可取消状态失败\n");
        return  NULL;
    }
    //设置线程接收到 Cancel 信号后立即结束执行
    res = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    if (res != 0) {
        printf("修改线程响应 Cancel 信号的方式失败\n");
        return  NULL;
    }
    while (1);
    return NULL;
}
int main()
{
    pthread_t myThread;
    void * mess;
    int value;
    int res;
    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;
    }
    //等待 myThread 线程执行结束,获取返回值
    res = pthread_join(myThread, &mess);
    if (res != 0) {
        printf("等待线程失败\n");
        return 0;
    }
    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 线程被强制终止

上面是在linux上的执行结果,在Windows下的执行结果如下图

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210711100722597.png
暂时还不知道是因为啥导致了这种结果,推测是因为运行环境不同,这边就先挖个坑,等后面了解了再来解决。

和《终止线程执行(3种方法)》一节中 pthread_cancel() 函数的演示程序相比,我们仅仅是将 myThread 线程设置为“接收到 Cancel 信号后立即结束执行”。通过对比两个程序的输出结果,很容易就可以体会出 pthread_setcancelstate() 和 pthread_setcanceltype() 函数的功能。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值