📄 pthread_cancel.man
字号:
.TH PTHREAD_CANCEL 3 LinuxThreads.XREF pthread_setcancelstate.XREF pthread_setcanceltype.XREF pthread_testcancel.SH NAMEpthread_cancel, pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel \- thread cancellation.SH SYNOPSIS#include <pthread.h>int pthread_cancel(pthread_t thread);int pthread_setcancelstate(int state, int *oldstate);int pthread_setcanceltype(int type, int *oldtype);void pthread_testcancel(void);.SH DESCRIPTIONCancellation is the mechanism by which a thread can terminate theexecution of another thread. More precisely, a thread can send acancellation request to another thread. Depending on its settings, thetarget thread can then either ignore the request, honor itimmediately, or defer it till it reaches a cancellation point.When a thread eventually honors a cancellation request, it performs asif !pthread_exit(PTHREAD_CANCELED)! has been called at that point:all cleanup handlers are executed in reverse order, finalizationfunctions for thread-specific data are called, and finally the threadstops executing with the return value !PTHREAD_CANCELED!. See!pthread_exit!(3) for more information.!pthread_cancel! sends a cancellation request to the thread denotedby the |thread| argument.!pthread_setcancelstate! changes the cancellation state for thecalling thread -- that is, whether cancellation requests are ignoredor not. The |state| argument is the new cancellation state: either!PTHREAD_CANCEL_ENABLE! to enable cancellation, or!PTHREAD_CANCEL_DISABLE! to disable cancellation (cancellationrequests are ignored). If |oldstate| is not !NULL!, the previouscancellation state is stored in the location pointed to by |oldstate|,and can thus be restored later by another call to!pthread_setcancelstate!.!pthread_setcanceltype! changes the type of responses to cancellationrequests for the calling thread: asynchronous (immediate) or deferred.The |type| argument is the new cancellation type: either!PTHREAD_CANCEL_ASYNCHRONOUS! to cancel the calling thread as soon asthe cancellation request is received, or !PTHREAD_CANCEL_DEFERRED! tokeep the cancellation request pending until the next cancellationpoint. If |oldtype| is not !NULL!, the previouscancellation state is stored in the location pointed to by |oldtype|,and can thus be restored later by another call to!pthread_setcanceltype!.Threads are always created by !pthread_create!(3) with cancellationenabled and deferred. That is, the initial cancellation state is!PTHREAD_CANCEL_ENABLE! and the initial type is!PTHREAD_CANCEL_DEFERRED!.Cancellation points are those points in the program execution where atest for pending cancellation requests is performed and cancellationis executed if positive. The following POSIX threads functionsare cancellation points:!pthread_join!(3).br!pthread_cond_wait!(3).br!pthread_cond_timedwait!(3).br!pthread_testcancel!(3).br!sem_wait!(3).br!sigwait!(3)All other POSIX threads functions are guaranteed not to becancellation points. That is, they never perform cancellation indeferred cancellation mode.!pthread_testcancel! does nothing except testing for pendingcancellation and executing it. Its purpose is to introduce explicitchecks for cancellation in long sequences of code that do not callcancellation point functions otherwise..SH "RETURN VALUE"!pthread_cancel!, !pthread_setcancelstate! and!pthread_setcanceltype! return 0 on success and a non-zero error codeon error..SH ERRORS!pthread_cancel! returns the following error code on error:.RS.TP!ESRCH!no thread could be found corresponding to that specified by the |thread| ID..RE!pthread_setcancelstate! returns the following error code on error:.RS.TP!EINVAL!the |state| argument is not !PTHREAD_CANCEL_ENABLE! nor!PTHREAD_CANCEL_DISABLE!.RE!pthread_setcanceltype! returns the following error code on error:.RS.TP!EINVAL!the |type| argument is not !PTHREAD_CANCEL_DEFERRED! nor!PTHREAD_CANCEL_ASYNCHRONOUS!.RE.SH AUTHORXavier Leroy <Xavier.Leroy@inria.fr>.SH "SEE ALSO"!pthread_exit!(3),!pthread_cleanup_push!(3),!pthread_cleanup_pop!(3)..SH BUGSPOSIX specifies that a number of system calls (basically, allsystem calls that may block, such as !read!(2), !write!(2), !wait!(2),etc.) and library functions that may call these system calls (e.g.!fprintf!(3)) are cancellation points. LinuxThreads is not yetintegrated enough with the C library to implement this, and thus noneof the C library functions is a cancellation point.For system calls at least, there is a workaround. Cancellationrequests are transmitted to the target thread by sending it asignal. That signal will interrupt all blocking system calls, causingthem to return immediately with the !EINTR! error. So, checking forcancellation during a !read! system call, for instance, can beachieved as follows:.RS.ft 3.nf.sppthread_testcancel();retcode = read(fd, buffer, length);pthread_testcancel();.ft.LP.RE.fi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -