linux下pthread_cancel无法取消线程的原因【转】
转自:http://blog.csdn.net/huangshanchun/article/details/47420961
版权声明:欢迎转载,如有不足之处,恳请斧正。
一个线程可以调用pthread_cancel终止同一进程中的另一个线程,但是值得强调的是:同一进程的线程间,pthread_cancel向另一线程发终止信号。系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程。或调用pthread_testcancel,让内核去检测是否需要取消当前线程。被取消的线程,退出值,定义在Linux的pthread库中常数PTHREAD_CANCELED的值是-1。
- #include <pthread.h>
- int pthread_cancel(pthread_t thread);
看下面程序:
- #include<stdio.h>
- #include<stdlib.h>
- #include <pthread.h>
- void *thread_fun(void *arg)
- {
- int i=1;
- printf("thread start \n");
- while(1)
- {
- i++;
- }
- return (void *)0;
- }
- int main()
- {
- void *ret=NULL;
- int iret=0;
- pthread_t tid;
- pthread_create(&tid,NULL,thread_fun,NULL);
- sleep(1);
- pthread_cancel(tid);//取消线程
- pthread_join(tid, &ret);
- printf("thread 3 exit code %d\n", (int)ret);
- return 0;
- }
会发现程序再一直运行,线程无法被取消,究其原因pthread_cancel向另一线程发终止信号。系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程。如果线程里面没有执行系统调用,可以使用pthread_testcancel解决。
- #include<stdio.h>
- #include<stdlib.h>
- #include <pthread.h>
- void *thread_fun(void *arg)
- {
- int i=1;
- printf("thread start \n");
- while(1)
- {
- i++;
- pthread_testcancel();
- }
- return (void *)0;
- }
- int main()
- {
- void *ret=NULL;
- int iret=0;
- pthread_t tid;
- pthread_create(&tid,NULL,thread_fun,NULL);
- sleep(1);
- pthread_cancel(tid);//取消线程
- pthread_join(tid, &ret);
- printf("thread 3 exit code %d\n", (int)ret);
- return 0;
- }
linux下pthread_cancel无法取消线程的原因【转】的更多相关文章
- linux下pthread_cancel无法取消线程的原因
一个线程能够调用pthread_cancel终止同一进程中的还有一个线程,可是值得强调的是:同一进程的线程间,pthread_cancel向还有一线程发终止信号.系统并不会立即关闭被取消线程,仅仅有在 ...
- Linux下c开发 之 线程通信(转)
Linux下c开发 之 线程通信(转) 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linu ...
- Linux下的进程与线程(二)—— 信号
Linux进程之间的通信: 本文主要讨论信号问题. 在Linux下的进程与线程(一)中提到,调度器可以用中断的方式调度进程. 然而,进程是怎么知道自己需要被调度了呢?是内核通过向进程发送信号,进程才得 ...
- linux下进程的最大线程数、进程最大数、进程打开的文件数
linux下进程的最大线程数.进程最大数.进程打开的文件数 ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...
- Linux下c开发 之 线程通信
Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...
- Linux学习笔记23——取消线程
一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...
- Linux下的进程与线程(一)—— 进程概览
进程是操作系统分配资源的基本单位.线程是操作系统进行运行和调度的基本单位. 进程之间可以切换,以便轮流占用CPU,实现并发.一般进程运行在用户模式下,只能执行指令集中的部分指令. 当进程进行上下文切换 ...
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...
- Linux下查看进程和线程
在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...
随机推荐
- udp->ip & tcp->ip 发送数据包的目的地址的源地址是什么时候确定的?
udp->ip & tcp->ip udp到ip层是:ip_send_skb tcp到ip层是: ip_queue_xmit 拿tcp为例,在使用[ip_queue_xmit, i ...
- 【EF】EF实现大批量数据库插入操作
通过参考http://www.itnose.net/news/171/6306259里面介绍的方法,我们可以在程序的解决方案的引用中选择NuGet管理包中添加 该扩展方法提供BulkInsert() ...
- 【Python】python基础语法 编码
编码 默认情况下,python以UTF-8编码,所有的字符串都是Unicode字符串,可以为代码定义不同的的编码. #coding:UTF-8 #OR #-*- coding:UTF-8 -*- p ...
- Oracle数据库中心双活之道:ASM vs VPLEX
Oracle数据库中心双活之道:ASM vs VPLEX 来源 https://www.cnblogs.com/wenjiewang/p/7460212.html 双活方案对比:ASM vs V-PL ...
- BZOJ1013:[JSOI2008]球形空间产生器——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1013 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在 ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- Vue项目搭建过程
环境搭建:mac+nodejs+npm #安装node.js : $ brew install node #安装vue-cil: $ npm install -g vue-cli 注:官网下载安装no ...
- 去掉vue 中的代码规范检测(Eslint验证)
去掉vue 中的代码规范检测(Eslint验证): 1.在搭建vue脚手架时提示是否启用eslint检测的. Use ESLint to lint your code? 写 no; 2.如果项目已经生 ...
- Nginx配置(一)
下载源码安装包:http://nginx.org 稳定版Nginx 1.6.2 tengine: 2.1.2 1.安装缺少依赖的包: (yum install jemalloc) yum -y ins ...
- book_notes
http://139.196.8.158/ https://caomall.worktile.com/tasks/projects/58fd73047619c44427c0d719 http://lo ...