热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

pthread_cancel、pthread_equal函数

(1)pthread_cancel函数intpthread_cancel(pthread_tthread);成功:0;失败

(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
#include
#include
#include 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 类型被修改为结构体实现,因此==就无法直接判断。


推荐阅读
author-avatar
手机用户2502857731
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有