转自:http://blog.csdn.net/huangshanchun/article/details/47420961

版权声明:欢迎转载,如有不足之处,恳请斧正。

一个线程可以调用pthread_cancel终止同一进程中的另一个线程,但是值得强调的是:同一进程的线程间,pthread_cancel向另一线程发终止信号。系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程。或调用pthread_testcancel,让内核去检测是否需要取消当前线程。被取消的线程,退出值,定义在Linux的pthread库中常数PTHREAD_CANCELED的值是-1。

  1. #include <pthread.h>
  2. int pthread_cancel(pthread_t thread);

看下面程序:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <pthread.h>
  4. void *thread_fun(void *arg)
  5. {
  6. int i=1;
  7. printf("thread start \n");
  8. while(1)
  9. {
  10. i++;
  11. }
  12. return (void *)0;
  13. }
  14. int main()
  15. {
  16. void *ret=NULL;
  17. int iret=0;
  18. pthread_t tid;
  19. pthread_create(&tid,NULL,thread_fun,NULL);
  20. sleep(1);
  21. pthread_cancel(tid);//取消线程
  22. pthread_join(tid, &ret);
  23. printf("thread 3 exit code %d\n", (int)ret);
  24. return 0;
  25. }

会发现程序再一直运行,线程无法被取消,究其原因pthread_cancel向另一线程发终止信号。系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程。如果线程里面没有执行系统调用,可以使用pthread_testcancel解决。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <pthread.h>
  4. void *thread_fun(void *arg)
  5. {
  6. int i=1;
  7. printf("thread start \n");
  8. while(1)
  9. {
  10. i++;
  11. pthread_testcancel();
  12. }
  13. return (void *)0;
  14. }
  15. int main()
  16. {
  17. void *ret=NULL;
  18. int iret=0;
  19. pthread_t tid;
  20. pthread_create(&tid,NULL,thread_fun,NULL);
  21. sleep(1);
  22. pthread_cancel(tid);//取消线程
  23. pthread_join(tid, &ret);
  24. printf("thread 3 exit code %d\n", (int)ret);
  25. return 0;
  26. }

linux下pthread_cancel无法取消线程的原因【转】的更多相关文章

  1. linux下pthread_cancel无法取消线程的原因

    一个线程能够调用pthread_cancel终止同一进程中的还有一个线程,可是值得强调的是:同一进程的线程间,pthread_cancel向还有一线程发终止信号.系统并不会立即关闭被取消线程,仅仅有在 ...

  2. Linux下c开发 之 线程通信(转)

    Linux下c开发 之 线程通信(转) 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linu ...

  3. Linux下的进程与线程(二)—— 信号

    Linux进程之间的通信: 本文主要讨论信号问题. 在Linux下的进程与线程(一)中提到,调度器可以用中断的方式调度进程. 然而,进程是怎么知道自己需要被调度了呢?是内核通过向进程发送信号,进程才得 ...

  4. linux下进程的最大线程数、进程最大数、进程打开的文件数

    linux下进程的最大线程数.进程最大数.进程打开的文件数   ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...

  5. Linux下c开发 之 线程通信

    Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...

  6. Linux学习笔记23——取消线程

    一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...

  7. Linux下的进程与线程(一)—— 进程概览

    进程是操作系统分配资源的基本单位.线程是操作系统进行运行和调度的基本单位. 进程之间可以切换,以便轮流占用CPU,实现并发.一般进程运行在用户模式下,只能执行指令集中的部分指令. 当进程进行上下文切换 ...

  8. 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程

    一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...

  9. Linux下查看进程和线程

    在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...

随机推荐

  1. udp->ip & tcp->ip 发送数据包的目的地址的源地址是什么时候确定的?

    udp->ip & tcp->ip udp到ip层是:ip_send_skb tcp到ip层是: ip_queue_xmit 拿tcp为例,在使用[ip_queue_xmit, i ...

  2. 【EF】EF实现大批量数据库插入操作

    通过参考http://www.itnose.net/news/171/6306259里面介绍的方法,我们可以在程序的解决方案的引用中选择NuGet管理包中添加  该扩展方法提供BulkInsert() ...

  3. 【Python】python基础语法 编码

    编码 默认情况下,python以UTF-8编码,所有的字符串都是Unicode字符串,可以为代码定义不同的的编码. #coding:UTF-8 #OR #-*- coding:UTF-8 -*-  p ...

  4. Oracle数据库中心双活之道:ASM vs VPLEX

    Oracle数据库中心双活之道:ASM vs VPLEX 来源 https://www.cnblogs.com/wenjiewang/p/7460212.html 双活方案对比:ASM vs V-PL ...

  5. BZOJ1013:[JSOI2008]球形空间产生器——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1013 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在 ...

  6. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  7. Vue项目搭建过程

    环境搭建:mac+nodejs+npm #安装node.js : $ brew install node #安装vue-cil: $ npm install -g vue-cli 注:官网下载安装no ...

  8. 去掉vue 中的代码规范检测(Eslint验证)

    去掉vue 中的代码规范检测(Eslint验证): 1.在搭建vue脚手架时提示是否启用eslint检测的. Use ESLint to lint your code? 写 no; 2.如果项目已经生 ...

  9. Nginx配置(一)

    下载源码安装包:http://nginx.org 稳定版Nginx 1.6.2 tengine: 2.1.2 1.安装缺少依赖的包: (yum install jemalloc) yum -y ins ...

  10. book_notes

    http://139.196.8.158/ https://caomall.worktile.com/tasks/projects/58fd73047619c44427c0d719 http://lo ...