pthread_cancel、pthread_equal函数

(1)pthread_cancel函数

int pthread_cancel(pthread_t thread);    成功:0;失败:错误号

作用:杀死(取消)线程,其作用对应进程中 kill() 函数。

注意:线程的取消并不是实时的,而有一定的延时。需要等待线程到达某个取消点(检查点)。杀死线程不是立刻就能完成,必须要到达取消点取消点:是线程检查是否被取消,并按请求进行动作的一个位置。通常是一些系统调用creat,open,pause,close,read,write等等。执行命令man 7 pthreads可以查看具备这些取消点的系统调用列表。也可参阅 APUE.12.7 取消选项小节。

可粗略认为一个系统调用(进入内核)即为一个取消点。如线程中没有取消点,可以通过调用pthread_testcancel( )函数自行设置一个取消点,即在子线程的执行的函数中调用该函数即可:pthread_testcancel( );  该函数是库函数,但执行该库函数需要进一步使用系统调用,从而到达检查点。只要线程到达检查点,如果有其它线程对其调用了pthread_cancel函数,则该线程就会自动终止。

被取消的线程,退出值为常数PTHREAD_CANCELED的值是-1。可在头文件pthread.h中找到它的定义#define PTHREAD_CANCELED ((void *) -1)。因此当我们对一个已经被取消的线程使用pthread_join回收时,得到的返回值为-1。

//终止线程的三种方法。注意“取消点”的概念。

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


void *tfn1(void *arg)   //方法1:retun
{
        printf("thread 1 returning\n");

        return (void *)111;
}

void *tfn2(void *arg)    //方法2:pthread_exit
{
        printf("thread 2 exiting\n");
        pthread_exit((void *)222);
}

void *tfn3(void *arg)   //方法三:pthread_cancel
{
        while (1) {
                 printf("thread 3: I'm going to die in 3 seconds ...\n");  //取消点
                 sleep(1);       //同样是取消点   

                //pthread_testcancel();   //自己添加取消点
        }

    return (void *)666;
}

int main(void)
{
        pthread_t tid;
        void *tret = NULL;

        pthread_create(&tid, NULL, tfn1, NULL);
        pthread_join(tid, &tret);
        printf("thread 1 exit code = %d\n\n", (int)tret);

        pthread_create(&tid, NULL, tfn2, NULL);
        pthread_join(tid, &tret);
        printf("thread 2 exit code = %d\n\n", (int)tret);

        pthread_create(&tid, NULL, tfn3, NULL);
        sleep(3);
        pthread_cancel(tid);
        pthread_join(tid, &tret);
        printf("thread 3 exit code = %d\n", (int)tret);

        return 0;
}

[root@localhost 01_pthread_test]# ./pthrd_endof3

thread 1 returning

thread 1 exit code = 111

 

thread 2 exiting

thread 2 exit code = 222

 

thread 3: I'm going to die in 3 seconds ...

thread 3: I'm going to die in 3 seconds ...

thread 3: I'm going to die in 3 seconds ...

thread 3 exit code = -1

分析:

  1. 线程3执行的函数的函数体中的printf函数会调用write系统调用;sleep函数会调用pause系统调用;而pthread_testcancel也会调用系统调用,因此这三个函数都是取消点;
  2. 如果线程3执行的函数的函数体中的while循环体为空(即删除上述三个函数),则该子线程将会永远不会被杀死,而主控线程将会永远阻塞等待回收子线程。这都是由于子线程没有取消点的原因,因此可以自己加取消点:pthread_testcancel。
  3. 终止某个线程而不终止整个进程,有三种方法:从线程主函数return,这种方法对主控线程不适用,从main函数return相当于调用exit;一个线程可以调用pthread_cancel终止同一进程中的另一个线程;线程可以调用pthread_exit终止自己。

(2)pthread_equal函数

int pthread_equal(pthread_t t1, pthread_t t2);

作用:比较两个线程ID是否相等。

返回值:相等返回非0值,不等返回0值。

由于目前的Linux系统中,线程ID类型为大于0的整数,因此可以用==直接比较是否相等;设置该函数是因为有可能Linux在未来线程ID pthread_t 类型被修改为结构体实现,因此==就无法直接判断。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值