12.7    取消一个线程

有时,想让一个线程能够要求还有一个线程终止,就像给它发送一个信号一样。

线程有方法能够做到这一点,与与信号处理一样。线程能够被要求终止时改变其行为。

pthread_cancel是用于请求一个线程终止的函数

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

这个函数提供一个线程标识符就能够发送请求来取消它。

线程能够用pthread_setcancelstate设置线程的取消状态

#include <pthread.h>
int pthread_setcancelstate(int state, int *oldstate);

第一个參数的取值能够是PTHREAD_CANCEL_ENABLE,这个值同意线程接收取消请求;或者是PTHREAD_CANCEL_DISABLE,它的作用是忽略取消请求。oldstate指针用于获取先前的取消状态。

假设取消请求被接受了,线程就能够进入第二个控制层次,用pthread_setcanceltype设置取消类型

#include <pthread.h>
int pthread_setcanceltype(int type, int *oldtype);

type參数能够有两种取值:一个是PTHREAD_CANCEL_ASYNCHRONOUR,它将使得在接收到取消请求后马上採取行动;还有一个是PTHREAD_CANCEL_DEFERRED,它将使得在接受到取消请求后,一直等待直到线程运行下述函数之中的一个才採取行动。详细是函数pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait或sigwait.

编敲代码thread7.c,主线程向它创建的线程发送一个取消请求。

/*************************************************************************
> File Name: thread7.c
> Description: thread7.c程序在主线程中向它创建的新线程发送一个取消请求
> Author: Liubingbing
> Created Time: 2015/7/7 9:58:40
> Other: thread7.c程序中新线程的调用函数中分别须要设置新线程的取消状态和取消类型
************************************************************************/ #include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h> void *thread_function(void *arg); int main(){
int res;
pthread_t a_thread;
void *thread_result;
/* pthread_create函数创建新线程,新线程标识符保存在a_thread。新线程调用的函数为thread_function,函数的參数为NULL */
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
} sleep(3);
printf("Canceling thread...\n");
/* pthread_cancel函数请求线程a_thread终止 */
res = pthread_cancel(a_thread);
if (res != 0) {
perror("Thread cancelation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
/* pthread_join等待线程a_thread与主线程又一次合并 */
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
} void *thread_function(void *arg) {
int i, res;
/* pthread_setcancelstate函数设置线程的取消状态,PTHREAD_CANCEL_ENABLE同意线程接收取消请求。PTHREAD_CANCEL_DISABLE忽略取消请求 */
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (res != 0) {
perror("Thread pthread_setcancelstate failed");
exit(EXIT_FAILURE);
}
/* pthread_setcanceltype函数设置线程的取消类型,PTHREAD_CANCEL_DEFERRED将使得在接收到取消请求后。一直等待直到线程运行某个函数(如pthread_join)之后才採取行动 */
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
if (res != 0) {
perror("Thread pthread_setcanceltype failed");
exit(EXIT_FAILURE);
}
printf("thread_function is running\n");
for (i = 0; i < 10; i++){
printf("Thread is still running (%d)...\n", i);
sleep(1);
}
pthread_exit(0);
}

以通常的方法创建新线程后,主线程休眠一会儿(好让新线程有时间開始运行),然后发送一个取消请求。例如以下所看到的:

sleep(3);
printf("Canceling thread...\n");
res = pthread_cancel(a_thread);

在新创建的线程中。首先将取消状态设置为同意取消,例如以下所看到的:

res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

然后将取消类型设置为延迟取消,例如以下所看到的:

res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);

最后。线程在循环中等待被取消,例如以下所看到的:

for (i = 0; i < 10; i++) {
printf("Thread is still running (%d)...\n", i);
sleep(1);
}

linux程序设计——取消一个线程(第十二章)的更多相关文章

  1. 深入理解JVM - Java内存模型与线程 - 第十二章

    Java内存模型 主内存与工作内存 Java内存模型主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节.此处的变量(Variable)与Java编程中 ...

  2. 《linux程序设计》--读书笔记--第十四章信号量、共享内存和消息队列

    信号量:用于管理对资源的访问: 共享内存:用于在程序之间高效的共享数据: 消息队列:在程序之间传递数据的一种简单方法: 一.信号量 临界代码:需要确保只有一个进程或者一个执行线程可以进入这个临界代码并 ...

  3. linux程序设计——网络信息(第十五章)

    15.3    网络信息 当眼下为止,客户和server程序一直是吧地址和port号编译到它们自己的内部. 对于一个更通用的server和客户程序来说.能够通过网络信息函数来决定应该使用的地址和por ...

  4. 201871010106-丁宣元 《面向对象程序设计(java)》第十二周学习总结

    201871010106-丁宣元 <面向对象程序设计(java)>第十二周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...

  5. linux设备驱动归纳总结(十二):简单的数码相框【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-116926.html linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxx ...

  6. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

  7. 【Linux开发】linux设备驱动归纳总结(十二):简单的数码相框

    linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  8. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

  9. “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

随机推荐

  1. linux内核分析之缺页中断【转】

    转自:http://blog.csdn.net/bullbat/article/details/7108402 linux缺页异常程序必须能够区分由编程引起的异常以及由引用属于进程地址空间但还尚未分配 ...

  2. 传输网页数据的json与xml

    #转载请留言联系 1.json json是数据格式,经常用于在网络中,不同平台或者不同语言中进行数据的传输.json的文件后缀就是 .json.当然,也可以把json直接写在js文件中. json储存 ...

  3. 第一部分:MongoDB备忘录

    一.NoSQL 简介 Nosql的全称是Not Only Sql,这个概念早起就有人提出,在09年的时候比较火.Nosql指的是非关系型数据库,而我们常用的都是关系型数据库.就像我们常用的mysql, ...

  4. hdu 5109(构造数+对取模的理解程度)

    Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. 关于delphi编程的网络文件夹复制的代码精要

    首先必须引用windows api函数库 shellapi ***************************以下为复制文件夹的代码******************************** ...

  6. codeforces-505B

    题目连接:http://codeforces.com/contest/505/problem/B B. Mr. Kitayuta's Colorful Graph time limit per tes ...

  7. 牛客网练习赛18 A 【数论/整数划分得到乘积最大/快速乘】

    链接:https://www.nowcoder.com/acm/contest/110/A 来源:牛客网 题目描述 这题要你回答T个询问,给你一个正整数S,若有若干个正整数的和为S,则这若干的数的乘积 ...

  8. JDBC_PreparedStatement 防sql注入

    package songyan.jdbc.login.prepared; import java.sql.Connection; import java.sql.DriverManager; impo ...

  9. Linux Programmer's Manual --- reboot

    REBOOT(2) Linux Programmer's Manual REBOOT(2) NAME reboot - reboot or enable/disable Ctrl-Alt-Del SY ...

  10. eclipse中jar包打断点

    eclipse中jar包打断点 1. 下载工具 链接:http://pan.baidu.com/s/1dEF5tqL 密码:md4m 2. 增加jadeclipse功能 把 net.sf.jadcli ...