假设线程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 相关的更多相关文章

  1. 使用 pthread_cancel 引入的死锁问题

    先来说一下 pthread_cancel 基本概念. pthread_cancel 调用并不是强制终止线程,它只提出请求.线程如何处理 cancel 信号则由目标线程自己决定,可以是忽略.可以是立即终 ...

  2. 嵌入式单片机STM32应用技术(课本)

    目录SAIU R20 1 6 第1页第1 章. 初识STM32..................................................................... ...

  3. pthread_cancel

    #include <pthread.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h&g ...

  4. 线程取消 (pthread_cancel)

    线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...

  5. 线程正常终止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 ...

  6. Linux 线程取消(pthread_cancel)

    基本概念 pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(CancellationPoint).取消 ...

  7. java中的字符串相关知识整理

    字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...

  8. SQL Server相关书籍

    SQL Server相关书籍 (排名不分先后) Microsoft SQL Server 企业级平台管理实践 SQL Server 2008数据库技术内幕 SQL Server性能调优实战 SQL S ...

  9. dotNET跨平台相关文档整理

    一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的平台,在工作之余花了很多时间在M ...

随机推荐

  1. TCP_IP Sockets编程C语言实现第2版 源码下载

    原书网址:http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/textcode.html 源码下载: 链接:https://pan.baidu. ...

  2. arcgis python 把多个MXD批量导出一个PDF

    # -*- coding: cp936 -*- import arcpy, os, string #Read input parameters from script tool mxdList = s ...

  3. [Tex学习笔记]让项目编号从4开始

    微信扫描如上二维码关注跟锦数学微信公众账号. 详情请见那里.

  4. tensorflow查看ckpt各节点名称

    from tensorflow.python import pywrap_tensorflowimport os checkpoint_path=os.path.join('output/res101 ...

  5. osgViewer应用基础

    #ifdef _WIN32#include <windows.h>#endif#include <osgViewer/Viewer>#include <osgDB/Rea ...

  6. Linux-文件系统的简单操作

    文件系统的简单操作 磁盘与目录的容量:df.du df [option] [目录或文件名] 参数: -a:列出所有的文件系统,包括系统特有的/proc等系统 -k:以KB的容量显示各文件系统 -m:以 ...

  7. 13 Flutter仿京东商城项目 商品列表筛选以及上拉分页加载更多

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  8. DSS分发压力实验

    DSS分发压力实验 昨天为验证依托DSS搭建流媒体直播监控系统的可行性,及确定实时流画面出现严重花屏的原因,做了一个压力实验. 网络拓扑如图: 1.DVR上配置4路视频(CIF / 25fps / 1 ...

  9. JAVA 基础编程练习题48 【程序 48 加密】

    48 [程序 48 加密] 题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密 规则如下:每位数字都加上 5,然后用和除以 10 的余数代替该数字,再将第一位和第四位交 ...

  10. 用alert打印js对象

    用alert查看对象: function writeObj(obj){ var description = ""; for(var i in obj){ var property= ...