文章目录
man 3 pthread_testcancel
PTHREAD_TESTCANCEL(3) Linux Programmer's Manual PTHREAD_TESTCANCEL(3)
NAME
pthread_testcancel - request delivery of any pending cancellation request
//请求交付任何未决的 cancellation 请求
SYNOPSIS
#include <pthread.h>
void pthread_testcancel(void);
Compile and link with -pthread.
DESCRIPTION
Calling pthread_testcancel() creates a cancellation point within the calling thread, so that a thread that is otherwise executing code that contains no
cancellation points will respond to a cancellation request.
//调用 pthread_testcancel() 会在调用线程中创建一个取消点,
//以便执行不包含取消点的代码的线程将对取消请求做出响应。
If cancelability is disabled (using pthread_setcancelstate(3)), or no cancellation request is pending, then a call to pthread_testcancel() has no effect.
//如果可取消性被禁用(使用 pthread_setcancelstate(3)),
//或者没有取消请求处于挂起状态,则调用 pthread_testcancel() 无效。
RETURN VALUE
This function does not return a value. If the calling thread is canceled as a consequence of a call to this function, then the function does not return.
//此函数不返回值。 如果调用线程由于调用此函数而被取消,则该函数不会返回。
ERRORS
This function always succeeds.
//这个函数总是成功的。
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
┌─────────────────────┬───────────────┬─────────┐
│Interface │ Attribute │ Value │
├─────────────────────┼───────────────┼─────────┤
│pthread_testcancel() │ Thread safety │ MT-Safe │
└─────────────────────┴───────────────┴─────────┘
CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
EXAMPLE
See pthread_cleanup_push(3).
SEE ALSO
pthread_cancel(3), pthread_cleanup_push(3), pthread_setcancelstate(3), pthreads(7)
COLOPHON
This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version
of this page, can be found at http://www.kernel.org/doc/man-pages/.
Linux 2015-08-08 PTHREAD_TESTCANCEL(3)
Manual page pthread_testcancel(3) line 5/46 (END) (press h for help or q to quit)
文章目录
20230816
Linux C语言中的pthread_testcancel()函数
简介
在Linux环境下,C语言中pthread_testcancel()
函数用于请求传递任何待处理的取消请求。它是POSIX线程库(Pthreads)的一部分,该库是一个用于多线程编程的C语言接口。
本文将深入探讨pthread_testcancel()
函数的使用和实践,其中包括其定义、工作原理、使用场景和相关代码示例。我们还将对其进行比较,并且通过投票来收集读者对本话题的观点。
什么是“线程取消”?
取消线程是指终止一个正在运行的线程的操作。在多线程编程中,可能会遇到需要提前结束一个线程执行的情况,这就需要进行线程的取消。
在POSIX线程(也称为Pthreads)库中,可以使用pthread_cancel()
函数来请求取消一个线程。然而,这只是发出了一个取消请求,并不能保证线程立即被取消。线程必须自身检查是否有挂起的取消请求,并决定何时安全地终止执行。这个检查过程可以通过调用pthread_testcancel()
函数来完成。
值得注意的是,不同的系统对线程取消的处理方式可能会有所不同,某些系统甚至可能不支持线程取消。因此,在编写可移植代码时,应尽量避免依赖于线程的取消功能。
例如,以下是一个使用pthread_cancel()
和pthread_testcancel()
函数的简单示例:
#include <pthread.h>
#include <stdio.h>
void* threadFunc(void* arg) {
printf("Thread function started\n");
// The loop will run until this thread is cancelled
while (1) {
pthread_testcancel();
}
return NULL;
}
int main() {
pthread_t threadId;
// Create a new thread
if (pthread_create(&threadId, NULL, &threadFunc, NULL) != 0) {
printf("Failed to create thread\n");
return 1;
}
sleep(2);
// Cancel the thread
if (pthread_cancel(threadId) != 0) {
printf("Failed to cancel thread\n");
return 2;
}
printf("Cancelled the thread\n");
// Wait for the thread to terminate
if (pthread_join(threadId, NULL) != 0) {
printf("Failed to join thread\n");
return 3;
}
printf("Successfully joined with the cancelled thread\n");
return 0;
}
在这个示例中,主线程在一段时间后发送取消请求给新创建的线程,该线程在其无限循环中周期性地调用pthread_testcancel()
以检查是否有挂起的取消请求,如果有,则它会停止执行并退出。
pthread_testcancel()函数的定义与工作原理
函数定义
pthread_testcancel()
函数的声明如下:
void pthread_testcancel(void);
此函数没有参数,也不返回任何值。
工作原理
当调用pthread_testcancel()
函数时,它会检查是否有挂起的取消请求。如果有,则该线程会在pthread_testcancel()
被调用的地方停止执行并退出。这个过程通常称为“线程取消”。
线程可以通过调用pthread_cancel()
函数来请求取消其他线程。然而,这只是发送了一个请求,并不能保证线程立即取消。线程必须周期性地检查是否有挂起的取消请求,这就是pthread_testcancel()
函数的作用。
使用pthread_testcancel()函数
以下是一个使用pthread_testcancel()
函数的简单示例:
#include <pthread.h>
#include <stdio.h>
void* threadFunc(void* arg) {
printf("Thread function started\n");
// The loop will run until this thread is cancelled
while (1) {
pthread_testcancel();
}
return NULL;
}
int main() {
pthread_t threadId;
// Create a new thread
if (pthread_create(&threadId, NULL, &threadFunc, NULL) != 0) {
printf("Failed to create thread\n");
return 1;
}
sleep(2);
// Cancel the thread
if (pthread_cancel(threadId) != 0) {
printf("Failed to cancel thread\n");
return 2;
}
printf("Cancelled the thread\n");
// Wait for the thread to terminate
if (pthread_join(threadId, NULL) != 0) {
printf("Failed to join thread\n");
return 3;
}
printf("Successfully joined with the cancelled thread\n");
return 0;
}
在这个示例中,我们创建了一个新线程,该线程只会在收到取消请求时才会退出。然后,我们在主线程中取消这个线程,并等待它终止。
pthread_testcancel()
函数和pthread_setcancelstate()
函数
pthread_setcancelstate()
函数允许线程禁用或启用取消。这是另一种控制线程取消的方法,但它与pthread_testcancel()
函数略有不同。
使用pthread_setcancelstate()
函数,线程可以设置自己的取消状态为PTHREAD_CANCEL_DISABLE
或PTHREAD_CANCEL_ENABLE
。如果设置为PTHREAD_CANCEL_DISABLE
,那么即使其他线程请求取消该线程,也不会立即发生。相反,取消请求将保持挂起状态,直到线程再次启用取消。
尽管pthread_setcancelstate()
函数提供了对取消更精细的控制,但pthread_testcancel()
函数仍然很有用。当线程在长时间运行的任务中周期性地调用pthread_testcancel()
时,这样做可以确保线程及时响应取消请求。
参考资料:
- POSIX Threads Programming (https://computing.llnl.gov/tutorials/pthreads/)
- pthread_testcancel - test for a pending cancellation request (https://man7.org/linux/man-pages/man3/pthread_testcancel.3.html)
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ