Linux学习笔记23——取消线程
一 相关函数
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——取消线程的更多相关文章
- Linux学习笔记(23) Linux备份
1. 备份概述 Linux系统需要备份的数据有/root,/home,/var/spool/mail,/etc及日志等其他目录. 安装服务的数据需要备份,如apache需要备份的数据有配置文件.网页主 ...
- Linux 学习笔记
Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...
- Linux 学习笔记之超详细基础linux命令 Part 9
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 1
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 说明:主要是在REHL Server 6操作系统下进行的测试 --字符界面虚拟终端与图形界面之间的切 方法:[ ...
- JUC学习笔记——进程与线程
JUC学习笔记--进程与线程 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的进程与线程部分 我们会分为以下几部分进行介绍: 进程与线程 并发与并行 同步与异步 线程详解 进程与线程 ...
- linux —— 学习笔记(汇总)
笔记目录:一.系统知识 和 基本概念 二.常用操作 三.系统管理(内存.设备.服务等管理) ...
- deepin linux 学习笔记(二)——文本编辑器
目录 deepin linux 学习笔记(二)--文本编辑器 前言 nano 小巧的命令行编辑器 通用 编辑 定位 排版 配置 vim 思路独特的超级编辑器 命令模式 插入模式 底线模式(末行模式) ...
- Linux 学习笔记之超详细基础linux命令 Part 13
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 12
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...
随机推荐
- LINQ数据库连接对象制造工厂
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 清楚form表单数据的便捷jQuery之法
有时候可能需要实现这样的效果:注册表单或者地址表单等填写多个记录之后,想要清除重新填写,如果一个个删除非常麻烦,因此这时清除按钮非常必须.接下来为您详细介绍两个自己经历的便捷方法,需要了解的朋友参考下 ...
- 从1到n整数中1出现的次数
题目如题 如 5 中1出现的次数 为1 12中1出现的次数为5 public class NumberOf1Between1AndN { /* *输入一个整数n,求从1到n这N个十进制表示中1出现的次 ...
- Codeforces 549B Looksery Party
Looksery Party Solution: 仔细分析一下会发现每个人都会发一条消息给自己这个条件非常重要! 这个条件保证了一定会有解,而且解法也要从这里入手. 当我们拿到一个猜测的答案序列的时候 ...
- Codeforces 551E - GukiZ and GukiZiana(分块)
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
- 彻底弄清c标准库中string.h里的常用函数用法
在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...
- GoJS研究,简单图表制作。
话不多说,先上图 我在这个中加入了缩略图.鼠标放大缩小等功能. <!doctype html> <html> <head> <title>Flowcha ...
- javascript 一串DIV跟随鼠标移动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Centos6.4配置Fedora EPEL源附配置hop5.in源
查看系统版本 cat /etc/redhat-release 下载CentOS 版本所对应的EPEL 的版本 wget http://download.fedoraproject.org/pub/ep ...
- 2016030201 - github上创建项目
具体步骤如下: 前提是你已经由github账户,登陆你的账户后 步骤1:点击右下角+new repository 2.添加代码库描述内容,比如reposltory name 例如我创建的内容 3.点击 ...