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. Java解决跨域的方案

    在后台加上,在数据返回之前添加 response.setHeader("Access-Control-Allow-Origin","*"); 就可以了,前台不用 ...

  2. chanme的博客搬家了!

    一直以来都想自己租一台服务器,买个域名做一个自己的博客,但是由于时间和知识的关系,以前还不太知道怎么搭一个博客.终于我在上个礼拜成功的迈出了建站的第一步,然后陆陆续续的也将一些后续的步骤做好了.所以今 ...

  3. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. AC日记——可能的路径 51nod 1247

    可能的路径 思路: 看到题目想到gcd: 仔细一看是更相减损: 而gcd是更相减损的优化版: 所以,对于每组数据判断gcd是否相等就好: 来,上代码: #include <cstdio> ...

  5. 解决iOS10的Safari下Meta设置user-scalable=no无效的方法

    苹果为了提高Safari中网站的辅助功能,屏蔽了Meta下的user-scalable=no功能.所以在iOS10下面,就算加上user-scalable=no,Safari浏览器也能支持手动缩放. ...

  6. sublime text mac使用技巧

    工欲善其事,必先利其器 1.列选择 鼠标左键+OPTION 2.查找替换 COMMAND+OPTION+F 3.分屏 COMMAND+OPTION+数字,具体数字代表要分几个屏

  7. ()C# DataRow

    判断某列是否存在,返回bool dr.Table.Columns.Contains("水分含量")

  8. SpringCloud简介(一)

    一.SpringCloud简介 SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等 ...

  9. Linux漏洞建议工具Linux Exploit Suggester

     Linux漏洞建议工具Linux Exploit Suggester 在Linux系统渗透测试中,通常使用Nessus.OpenVAS对目标主机进行扫描,获取目标主机可能存在的漏洞.如果无法进行漏洞 ...

  10. Count Primes -- LeetCodes (primality test)

    Description: Count the number of prime numbers less than a non-negative number, n. 思路:这题第一种思路是写一个is_ ...