pthread_cancel 相关
假设线程A对线程B发出了一个取消请求。通过如下函数:
#include <pthread.h>
int pthread_cancel(pthread_t thread); 参数:
thread - 收到取消请求的线程id 返回值:
发送终止信号给thread线程,如果成功则返回0,否则为非0值。发送成功并不意味着thread会终止。
线程B如何反应呢?
这取决线程的两个状态属性,它们决定了该线程收到取消请求时的行为,下面给出这两个字段的意义和用法。
(1)线程的cancelability state
字段可以取值为:enabled (默认) or disabled。当取值为enabled,由cancelability type属性决定线程的行为。当取值为disabled,线程不可被取消,但取消请求还是会被接受。
#include <pthread.h>
int pthread_setcancelstate(int state, int *oldstate); 参数:
state - 设置线程的取消state,可以取如下值
PTHREAD_CANCEL_ENABLE(默认取值)
PTHREAD_CANCEL_DISABLE oldstate - 用作保存线程之前的取消state
(2)线程的cancelability type
#include <pthread.h>
int pthread_setcanceltype(int type, int *oldtype); 参数:
type - 取值如下
PTHREAD_CANCEL_DEFERRED(默认取值)
取消请求会被推迟到调用一个是cancellation point的函数,参看http://man7.org/linux/man-pages/man7/pthreads.7.html PTHREAD_CANCEL_ASYNCHRONOUS
线程可以在任何时候被取消。(通常,线程只要接收到取消请求,便会直接被取消。但是系统不保证这一点)。 oldtype - 保存旧的type
一个例子
#include <pthread.h>
#include <unistd.h>
#include <stdio.h> void wait() {
for(int i = ; i < ; i++){
for (int j = ; j < ; j++){
int k = ;
}
}
} void* PrintHello(void* threadid) {
wait();//延时,等待足够时间以至于能够使得这个线程接收到取消请求。不用sleep函数是因为,这里需要测试pthread_testcancel函数是否是取消点,然而sleep也是取消点 pthread_testcancel();// printf("hi\n"); //这一行不会被打印 pthread_exit(NULL);
} int main(){
pthread_t tid;
pthread_create(&tid, NULL, PrintHello, NULL);
pthread_cancel(tid);//对tid对应的线程发送一个取消请求 pthread_exit(NULL);
return ;
}
1.需要的头文件
#include <pthread.h>
2.编译指令
gcc main.c -lpthread
3.相关概念
http://man7.org/linux/man-pages/man3/pthread_setcancelstate.3.html
pthread_cancel 相关的更多相关文章
- 使用 pthread_cancel 引入的死锁问题
先来说一下 pthread_cancel 基本概念. pthread_cancel 调用并不是强制终止线程,它只提出请求.线程如何处理 cancel 信号则由目标线程自己决定,可以是忽略.可以是立即终 ...
- 嵌入式单片机STM32应用技术(课本)
目录SAIU R20 1 6 第1页第1 章. 初识STM32..................................................................... ...
- pthread_cancel
#include <pthread.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h&g ...
- 线程取消 (pthread_cancel)
线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread); void pthrea ...
- Linux 线程取消(pthread_cancel)
基本概念 pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(CancellationPoint).取消 ...
- java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...
- SQL Server相关书籍
SQL Server相关书籍 (排名不分先后) Microsoft SQL Server 企业级平台管理实践 SQL Server 2008数据库技术内幕 SQL Server性能调优实战 SQL S ...
- dotNET跨平台相关文档整理
一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的平台,在工作之余花了很多时间在M ...
随机推荐
- 解决python 保存json到文件时 中文显示16进制编码的问题
python 2.7 import codecs import json with codecs.open('Options.json', 'w', encoding='utf-8') as f: j ...
- Projects: Linux scalability: Accept() scalability on Linux 惊群效应
小结: 1.不必要的唤醒 惊群效应 https://github.com/benoitc/gunicorn/issues/792#issuecomment-46718939 https://www.c ...
- [go]gin框架
gin参考 Gin框架返回值 // 返回json func main() { r := gin.Default() //方法一: 自己拼接json // gin.H is a shortcut for ...
- VBA添加下拉菜单
Sub createMenus() Dim cmdBar As CommandBar Dim cmdMenu As CommandBarPopup Dim cmdBtn As CommandBarBu ...
- produceTestDate
set serveroutput on --使用基本变量类型 declare --定义基本变量:类型 --基本数据类型 pnumber , ); pname ); pdate date; begin ...
- windows7安装docker异常:looks like something went wrong in step ‘looking for vboxmanage.exe’
一.背景 最近准备抽点时间研究下docker,选择在家中的windows系统上安装. 我的系统是windows7,首先安装Docker Toolbox,Docker Toolbox是一个工具集,主要包 ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_12.RabbitMQ研究-工作模式-统配符工作模式测试
路由模式: 1.每个消费者监听自己的队列,并且设置带统配符的routingkey. 2.生产者将消息发给broker,由交换机根据routingkey来转发消息到指定的队列. 创建测试用例 交换机的名 ...
- Vue的基础学习
一.Vue的计算属性:get和set属性 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 总是访问到tomcat首页解决
部署代码后总是访问到tomcat首页解决 没有把路径写全 访问:养成带上绝对路径的习惯,否则总是访问到tomcat的首页 http://114.116.65.232:8085/ssoserver/
- 如何将Nginx注册为系统服务,开机自启动
亲测有效! 一般程序员在实际工作中,除了敲代码,很少有机会实际接触操作其它东西,例如服务器环境搭建,项目部署等等,不是领导信任或项目组核心成员,应该是没有机会实际接触的,只能通过网上资料稍微了解一下. ...