pthread_cancel手册翻译
创始人
2024-04-02 16:31:36
0

原文链接:https://editor.csdn.net/md/?not_checkout=1&spm=1010.2135.3001.5352

NAME

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

SYNOPSIS

#include 
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 
#include 
#include 
#include 
#include #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/找到。

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...