转自: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. [剑指Offer] 46.孩子们的游戏

    题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...

  2. Access Denied for user root @localhost 解决方案

    问题描述: C:\Users\bo.wang> mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for us ...

  3. (一)MySQL学习笔记

    1.MySQL安装 下载地址:https://dev.mysql.com/downloads/mysql/ 启动mysql服务 net start mysql 停止mysql服务 net stop m ...

  4. [SOJ #48]集合对称差卷积

    题目大意:给你两个多项式$A,B$,求多项式$C$使得: $$C_n=\sum\limits_{x\oplus y=n}A_xB_y$$题解:$FWT$ 卡点:无 C++ Code: #include ...

  5. SPOJ694/DISUBSTR:Distinct Substrings——题解

    https://vjudge.net/problem/SPOJ-DISUBSTR https://www.luogu.org/problemnew/show/SP694 http://www.spoj ...

  6. ExtJs在页面上window再调用Window的事件处理

    今天在开发Ext的过程中遇到了一个恶心的问题,就是在ext.window页面,点击再次弹出window时,gridpanel中的store数据加载异常,不能正常被加载,会出现缓存,出现该问题,是因为w ...

  7. mysql定时器,定时查询数据库,把查询结果插入到一张表中

    1.有两张表order_repayment_detail,msg_sim ,需要把前者中的按时间过滤出来的信息插入到短信发送表中,可以每晚12点钟查询执行一次查询. 创建存储过程,这里的存储过程主要提 ...

  8. Ubuntu16.04 U盘安装Ubuntu16.04制作 光盘刻录 安装与简介

    从今天开始,我会把我遇到过的技术问题一一记录下来,从而分享给有需要的朋友,尽量希望你们少走弯路! 一.首先从官网上下载Ubuntu16.04镜像,下载最好从官网上下载(http://www.ubunt ...

  9. qt4+vs2010 环境搭建

    1.安装开发所需的软件: vs2010(包括VS2010SP1dvd1,Visual_Assist_X_10.9.2062.0_Crack等) QT: qt-win-opensource-4.8.5- ...

  10. [iptables]iptables 添加log到syslog

    比如iptables本来有这么一条: -A PREROUTING -d 125.65.27.xxx/32 -p tcp -m tcp --dport 11060 -j DNAT --to-destin ...