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)
提供了一份函数列表。
当对一个取消请求执行操作时,下面的步骤会发生(按顺序的):
- 取消清理程序(cancellation clean-up handlers)出栈(按照它们被入栈的相反顺序)并被调用。(参见
pthread_cleanup_push(3)
) - 线程特定的数据析构函数被调用——按照未指定的顺序。(参见
pthread_key_create(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/找到。