当前位置: 首页 > article >正文

pthread_cancel手册翻译

原文链接:https://blog.csdn.net/qq_51470638/article/details/127641034

NAME

pthread_cancel - 向线程发送取消请求。

SYNOPSIS

#include <pthread.h>
int pthrread_cancel(pthread_t thread);

编译,并用-pthread链接。

DESCRIPTION

pthread_cancel()函数向thread线程发送取消请求。至于是否以及何时目标线程会对取消请求做出响应,依赖于该线程控制下的两个属性: 它的可取消状态(cancelability state)和类型( type)。

cancelability state
  • 一个线程的可取消状态(cancelability state)(这由pthread_setcancelstate(3)确定),可以启用(新线程默认开启)或禁用。
  • 如果一个线程禁用了取消性(cancellation),那么一个取消请求会一直保留在队列里,直到这个线程启用取消性。
  • 如果一个线程已经启用了取消性,那么它的可取消类型(cancelabilty type)决定了何时取消发生。
cancelability type
  • 一个线程的可取消类型(cancelabilty type),可能是异步的(asynchronous)或延期的(deferred)(新线程默认为deferred)。
  • 异步取消性意味着:线程可以在任何时刻取消(通常是立刻,但是系统不保证是这样)。
  • 延期取消性意味着:取消将会推迟发生,直到下一次调用一个函数——这个函数得是一个取消点(cancellation point)。
  • 哪些函数可用作为取消点?这在pthreads(7)提供了一份函数列表。

当对一个取消请求执行操作时,下面的步骤会发生(按顺序的):

  1. 取消清理程序(cancellation clean-up handlers)出栈(按照它们被入栈的相反顺序)并被调用。(参见pthread_cleanup_push(3)
  2. 线程特定的数据析构函数被调用——按照未指定的顺序。(参见pthread_key_create(3)
  3. 线程终止。(参见pthread_exit(3)

对于pthread_cancel()上面的是异步发生的;pthread_cancel()的返回状态仅仅通知调用者:取消请求是否成功进入了取消队列。

在一个被取消的线程终结后,使用pthread_join(3)连接线程会获取PTHREAD_CANCELED作为这个线程的退出状态(连接线程是唯一可以知道取消已经完成了的途径)。

RETURN VALUE

成功返回0,失败返回非0;

ERRORS

ESRCH 找不到所给线程ID对应的线程。

ATTRIBUTES

有关本节使用的属于,参见attributes(7)

接口属性
pthread_cancel()线程安全性线程安全

CONFORMING TO

POSIX.1-2001, POSIX.1-2008.

NOTES

在Linux上,取消是通过信号来实现的。在NPTL线程实现中,第一个实时信号(即信号 32)用于这个目的。在Linux线程中,如果实时信号可用,那么第二个实时信号被使用,否则会使用SIGUSR2

EXAMPLES

下面的程序创建一个线程,然后取消它。主线程连接这个取消的来显来检查它的退出状态——应为PTHREAD_CANCELED。下面的shell会话显示了这个程序运行结果:

$ ./a.out
thread_func(): started; cancellation disabled
main(): sending cancellation request
thread_func(): about to enable cancellation
main(): thread was canceled
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>#define handle_error_en(en, msg) \do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)static void *
thread_func(void *ignored_argument)
{int s;/* Disable cancellation for a while, so that we don'timmediately react to a cancellation request */s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);if (s != 0)handle_error_en(s, "pthread_setcancelstate");printf("thread_func(): started; cancellation disabled\n");sleep(5);printf("thread_func(): about to enable cancellation\n");s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);if (s != 0)handle_error_en(s, "pthread_setcancelstate");/* sleep() is a cancellation point */sleep(1000);        /* Should get canceled while we sleep *//* Should never get here */printf("thread_func(): not canceled!\n");return NULL;
}int
main(void)
{pthread_t thr;void *res;int s;/* Start a thread and then send it a cancellation request */s = pthread_create(&thr, NULL, &thread_func, NULL);if (s != 0)handle_error_en(s, "pthread_create");sleep(2);           /* Give thread a chance to get started */printf("main(): sending cancellation request\n");s = pthread_cancel(thr);if (s != 0)handle_error_en(s, "pthread_cancel");/* Join with thread to see what its exit status was */s = pthread_join(thr, &res);if (s != 0)handle_error_en(s, "pthread_join");if (res == PTHREAD_CANCELED)printf("main(): thread was canceled\n");elseprintf("main(): thread wasn't canceled (shouldn't happen!)\n");exit(EXIT_SUCCESS);
}

SEE ALSO

pthread_cleanup_push(3), pthread_create(3), pthread_exit(3),
pthread_join(3), pthread_key_create(3), pthread_setcancelstate(3),
pthread_setcanceltype(3), pthread_testcancel(3), pthreads(7)

COLOPHON

此页面是 Linux man-pages project 5.10 的一部分。一个项目描述、关于报告错误的信息以及最新的此页面的版本,可以在https://www.kernel.org/doc/man-pages/找到。

http://www.lryc.cn/news/2420438.html

相关文章:

  • 微信小程序开发中的数据分析与统计
  • 出身比你好的人,比你聪明,还比你努力
  • Oracle数据库链Database links(dblinks)使用方法
  • Qt 防止程序多次运行
  • 器件选型篇:二极管之肖特基选型
  • 龙生九子 联想06年超越新机官方多图赏上篇
  • 无线震动传感器
  • 亚马逊云aws12个月免费服务器搭建小结
  • 多普达S900 使用详细说明(使用技巧大全)
  • 家中常用药
  • ADUM1201和川土微电子的CA-IS3722HS数字隔离器参数对比
  • 不喜欢stormliv.exe的最简单解决办法
  • 冷风机的安装
  • SEO按天计费系统源码 聚合SEO关键词按天自动扣费 | Thinkphp框架
  • 3d工口医Android,3d口工医完整版
  • Windows10系统安装软件时显示“无法访问Windows Installer服务。Windows Installer没有正确安装时可能发生这种情况。” or “错误2:系统找不到指定的文件”
  • 乌班图服务器系统升级,Ubuntu 13.04升级至Ubuntu 13.10(服务器版)教程
  • ARM烧录笔记
  • java7723魂斗罗2_魂斗罗3代-完全版
  • 课程设计-基于Springboot+Vue的大学生社团活动平台的设计与实现(源码+LW+包运行)
  • springboot房屋租赁系统-计算机毕业设计源码74728
  • 【Unity Shader入门精要 第3章】Unity Shader基础
  • 卡巴斯基kis6换许可文件key教程(转)
  • 基于Matlab的碎纸片的自动拼接复原技术
  • 电脑变慢怎么让电脑变快,教你从软硬件 两方面搞定
  • 智源:70万预算从头开发千亿参数大模型,挑战成功
  • 《黑马程序员》 category分类的使用(Objective - c语法)
  • 黑马程序员-JAVASE入门(其他对象)
  • Windows 11家庭版 中 VMware 中开启虚拟机的虚拟化引擎(开启虚拟化 IntelVT-x/EPT或AMD-V/RVI(V))
  • SQL Server Select Top 语句