一 相关函数

  1 发送终止信号

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

  2 设置取消状态

#include <pthread.h>
int pthread_setcancelstate(int state,    //取值:1 PTHREAD_CANCEL_ENABLE,这个值允许线程接受取消请求
                          //    2 PTHREAD_CANCEL_DISABLE,它的作用是忽略取消请求
                int *oldstate   //用于获取先前的取消状态
               );

  3 设置取消类型
  如果取消进程请求被接受了,线程可以进入第二个控制层次,用pthread_setcanceltype

#include <pthread.h>
int pthread_setcanceltype(int type,     //取值:1 PTHREAD_CANCEL_ASYNCHRONOUS,它将使得在接收到取消请求后立即采取行动
                         //   2 PTHREAD_CANCEL_DEFERRED,它将使得在接收到取消请求后,一直等待直到线程执行了下述函数之一后才采取行动
                               pthread_join,pthread_cond_wait,pthread_cond_timewait,pthread_testcancel,sem_wait或sigwait
               int *oldtype   //可以保存先前的状态,如果不想知道先前的状态,可以传递NULL给它
               );

  默认情况下,线程在启动时的取消状态为PTHREAD_CANCEL_ENABLE,取消类型是PTHREAD_CANCEL_DEFERRED

二 线程取消的语义

  线程取消的方法是向目标线程发Cancel信号(pthread_cancel函数发送Cancel信号),但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态(pthread_setcancelstate函数设置状态)决定。

  线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。

三 例子

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h> void *thread_function(void *arg); int main(){
pthread_t a_thread;
int res;
void *thread_result; res=pthread_create(&a_thread,NULL,thread_function,NULL);
if(res!=){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
sleep();      //主线程等待3秒
printf("Canceling thread...\n");
res=pthread_cancel(a_thread);  //发送取消线程请求
if(res!=){
perror("Thread canceling failed\n");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res=pthread_join(a_thread,&thread_result);    //终止线程
if(res!=){
perror("Thread join failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
} void *thread_function(void *arg){
int i,res;
res=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);    //设置取消状态为允许接受取消请求
if(res!=){
perror("Thread pthread_setcancelstate failed");
exit(EXIT_FAILURE);
}
res=pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);   //设置取消类型为等待执行函数后停止
if(res!=){
perror("Thread pthread_setcanceltype failed");
exit(EXIT_FAILURE);
}
printf("thread_function is running\n");
for(i=;i<;i++){
printf("Thread is still running (%d)...\n",i);
sleep();
}
pthread_exit();
}

Linux学习笔记23——取消线程的更多相关文章

  1. Linux学习笔记(23) Linux备份

    1. 备份概述 Linux系统需要备份的数据有/root,/home,/var/spool/mail,/etc及日志等其他目录. 安装服务的数据需要备份,如apache需要备份的数据有配置文件.网页主 ...

  2. Linux 学习笔记

    Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...

  3. Linux 学习笔记之超详细基础linux命令 Part 9

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...

  4. Linux 学习笔记之超详细基础linux命令 Part 1

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122   说明:主要是在REHL Server 6操作系统下进行的测试 --字符界面虚拟终端与图形界面之间的切 方法:[ ...

  5. JUC学习笔记——进程与线程

    JUC学习笔记--进程与线程 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的进程与线程部分 我们会分为以下几部分进行介绍: 进程与线程 并发与并行 同步与异步 线程详解 进程与线程 ...

  6. linux —— 学习笔记(汇总)

    笔记目录:一.系统知识 和 基本概念                    二.常用操作                   三.系统管理(内存.设备.服务等管理)                   ...

  7. deepin linux 学习笔记(二)——文本编辑器

    目录 deepin linux 学习笔记(二)--文本编辑器 前言 nano 小巧的命令行编辑器 通用 编辑 定位 排版 配置 vim 思路独特的超级编辑器 命令模式 插入模式 底线模式(末行模式) ...

  8. Linux 学习笔记之超详细基础linux命令 Part 13

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...

  9. Linux 学习笔记之超详细基础linux命令 Part 12

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...

随机推荐

  1. ipad屏幕旋转后的代理

    - (void)statusBarOrientationChange:(NSNotification *)notification {         UIInterfaceOrientation o ...

  2. Linux Bash环境下单引(')、双引号(")、反引号(`)、反斜杠(\)的小结

    在bash中,$.*.?.[.].’.”.`.\.有特殊的含义.类似于编译器的预编译过程,bash在扫描命令行的过程中,会在文本层次上,优先解释所有的特殊字符,之后对转换完成的新命令行,进行内核的系统 ...

  3. ACM HDU 2674 N! Again(数论)

    继续数论.. Problem Description WhereIsHeroFrom:            Zty,what are you doing ? Zty:                 ...

  4. Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld

    本文内容:cocos2dx+VS2013环境下创建项目,部分代码简析.会的朋友可以略过. 前面简单安装了几个环境,程序完全可以顺利跑起来(其他的cocos-stadio这些需要用到再装) 1.命令行形 ...

  5. 关于Apple设备私有的apple-touch-icon属性详解

    以前我们用过favicon在浏览器给网站进行身份标识,用法如下: <link href="http://image.feeliu.com/web/favicon.ico" r ...

  6. Zend Studio 11.0.2 破解和汉化

    本方法适用于Zend Studio 11.0.2,亲测,其他版本未知. 破解方法:覆盖安装目录 plugins 里同名文件,启动任意输入即可注册. Windows版下载地址:http://downlo ...

  7. C#程序中:如何向xml文件中写入数据和读取数据

    xml文件作为外部信息存储文件使用简单,方便,其结构和表格略有相似,下面简单的说一下xml文件内容的读取 …… using System.Xml;using System.IO;namespace W ...

  8. JQ插件ajaxFileUpload、php实现图片,数据同时上传

    代码结构如下: 1.HTML代码,没必要解释了. <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  9. jquery 效果

    效果1.基本效果    1.1 show([speed,[easing],[fn]]) 如果元素本身是可见的,则不对其作任何改变.如果元素是隐藏的,则使其可见.        $("p&qu ...

  10. jquery前端性能优化(持续添加。。。)

    1.选择器的使用 (1)$('#id')   使用id来定位dom元素是性能最高的方法.jQuery底层将直接调用本地方法document.getElementById().如果id直接可以找到所要对 ...