pthread_setcancelstate.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
  5. <TITLE>PTHREAD_CANCEL(3) manual page</TITLE>
  6. <META NAME="GENERATOR" CONTENT="OpenOffice.org 1.1.3 (Linux)">
  7. <META NAME="CREATED" CONTENT="20050504;12090500">
  8. <META NAME="CHANGED" CONTENT="20050504;16361300">
  9. <!-- manual page source format generated by PolyglotMan v3.2, -->
  10. <!-- available at http://polyglotman.sourceforge.net/ -->
  11. </HEAD>
  12. <BODY LANG="en-GB" BGCOLOR="#ffffff" DIR="LTR">
  13. <H4>POSIX Threads for Windows – REFERENCE - <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A></H4>
  14. <P><A HREF="index.html">Reference Index</A></P>
  15. <P><A HREF="#toc">Table of Contents</A></P>
  16. <H2><A HREF="#toc0" NAME="sect0">Name</A></H2>
  17. <P>pthread_cancel, pthread_setcancelstate, pthread_setcanceltype,
  18. pthread_testcancel - thread cancellation
  19. </P>
  20. <H2><A HREF="#toc1" NAME="sect1">Synopsis</A></H2>
  21. <P><B>#include &lt;pthread.h&gt;</B>
  22. </P>
  23. <P><B>int pthread_cancel(pthread_t </B><I>thread</I><B>);</B>
  24. </P>
  25. <P><B>int pthread_setcancelstate(int </B><I>state</I><B>, int
  26. *</B><I>oldstate</I><B>);</B>
  27. </P>
  28. <P><B>int pthread_setcanceltype(int </B><I>type</I><B>, int
  29. *</B><I>oldtype</I><B>);</B>
  30. </P>
  31. <P><B>void pthread_testcancel(void);</B>
  32. </P>
  33. <H2><A HREF="#toc2" NAME="sect2">Description</A></H2>
  34. <P>Cancellation is the mechanism by which a thread can terminate the
  35. execution of another thread. More precisely, a thread can send a
  36. cancellation request to another thread. Depending on its settings,
  37. the target thread can then either ignore the request, honor it
  38. immediately, or defer it until it reaches a cancellation point.
  39. </P>
  40. <P>When a thread eventually honors a cancellation request, it
  41. performs as if <B>pthread_exit(PTHREAD_CANCELED)</B> has been called
  42. at that point: all cleanup handlers are executed in reverse order,
  43. destructor functions for thread-specific data are called, and finally
  44. the thread stops executing with the return value <B>PTHREAD_CANCELED</B>.
  45. See <A HREF="pthread_exit.html"><B>pthread_exit</B>(3)</A> for more
  46. information.
  47. </P>
  48. <P><B>pthread_cancel</B> sends a cancellation request to the thread
  49. denoted by the <I>thread</I> argument.
  50. </P>
  51. <P><B>pthread_setcancelstate</B> changes the cancellation state for
  52. the calling thread -- that is, whether cancellation requests are
  53. ignored or not. The <I>state</I> argument is the new cancellation
  54. state: either <B>PTHREAD_CANCEL_ENABLE</B> to enable cancellation, or
  55. <B>PTHREAD_CANCEL_DISABLE</B> to disable cancellation (cancellation
  56. requests are ignored). If <I>oldstate</I> is not <B>NULL</B>, the
  57. previous cancellation state is stored in the location pointed to by
  58. <I>oldstate</I>, and can thus be restored later by another call to
  59. <B>pthread_setcancelstate</B>.
  60. </P>
  61. <P><B>pthread_setcanceltype</B> changes the type of responses to
  62. cancellation requests for the calling thread: asynchronous
  63. (immediate) or deferred. The <I>type</I> argument is the new
  64. cancellation type: either <B>PTHREAD_CANCEL_ASYNCHRONOUS</B> to
  65. cancel the calling thread as soon as the cancellation request is
  66. received, or <B>PTHREAD_CANCEL_DEFERRED</B> to keep the cancellation
  67. request pending until the next cancellation point. If <I>oldtype</I>
  68. is not <B>NULL</B>, the previous cancellation state is stored in the
  69. location pointed to by <I>oldtype</I>, and can thus be restored later
  70. by another call to <B>pthread_setcanceltype</B>.
  71. </P>
  72. <P><B>Pthreads-w32</B> provides two levels of support for
  73. <B>PTHREAD_CANCEL_ASYNCHRONOUS</B>: full and partial. Full support
  74. requires an additional DLL and driver be installed on the Windows
  75. system (see the See Also section below) that allows blocked threads
  76. to be cancelled immediately. Partial support means that the target
  77. thread will not cancel until it resumes execution naturally. Partial
  78. support is provided if either the DLL or the driver are not
  79. automatically detected by the pthreads-w32 library at run-time.</P>
  80. <P>Threads are always created by <A HREF="pthread_create.html"><B>pthread_create</B>(3)</A>
  81. with cancellation enabled and deferred. That is, the initial
  82. cancellation state is <B>PTHREAD_CANCEL_ENABLE</B> and the initial
  83. type is <B>PTHREAD_CANCEL_DEFERRED</B>.
  84. </P>
  85. <P>Cancellation points are those points in the program execution
  86. where a test for pending cancellation requests is performed and
  87. cancellation is executed if positive. The following POSIX threads
  88. functions are cancellation points:
  89. </P>
  90. <P><A HREF="pthread_join.html"><B>pthread_join</B>(3)</A>
  91. <BR><A HREF="pthread_cond_wait.html"><B>pthread_cond_wait</B>(3)</A>
  92. <BR><A HREF="pthread_cond_timedwait.html"><B>pthread_cond_timedwait</B>(3)</A>
  93. <BR><A HREF="pthread_testcancel.html"><B>pthread_testcancel</B>(3)</A>
  94. <BR><A HREF="sem_wait.html"><B>sem_wait</B>(3)</A> <BR><A HREF="sem_timedwait.html"><B>sem_timedwait</B>(3)</A>
  95. <BR><A HREF="sigwait.html"><B>sigwait</B>(3)</A> (not supported under
  96. <B>Pthreads-w32</B>)</P>
  97. <P><B>Pthreads-w32</B> provides two functions to enable additional
  98. cancellation points to be created in user functions that block on
  99. Win32 HANDLEs:</P>
  100. <P><A HREF="pthreadCancelableWait.html">pthreadCancelableWait()</A>
  101. <BR><A HREF="pthreadCancelableTimedWait.html">pthreadCancelableTimedWait()</A></P>
  102. <P>All other POSIX threads functions are guaranteed not to be
  103. cancellation points. That is, they never perform cancellation in
  104. deferred cancellation mode.
  105. </P>
  106. <P><B>pthread_testcancel</B> does nothing except testing for pending
  107. cancellation and executing it. Its purpose is to introduce explicit
  108. checks for cancellation in long sequences of code that do not call
  109. cancellation point functions otherwise.
  110. </P>
  111. <H2><A HREF="#toc3" NAME="sect3">Return Value</A></H2>
  112. <P><B>pthread_cancel</B>, <B>pthread_setcancelstate</B> and
  113. <B>pthread_setcanceltype</B> return 0 on success and a non-zero error
  114. code on error.
  115. </P>
  116. <H2><A HREF="#toc4" NAME="sect4">Errors</A></H2>
  117. <P><B>pthread_cancel</B> returns the following error code on error:
  118. </P>
  119. <DL>
  120. <DL>
  121. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>ESRCH</B>
  122. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  123. no thread could be found corresponding to that specified by the
  124. <I>thread</I> ID.
  125. </DD></DL>
  126. </DL>
  127. <P>
  128. <B>pthread_setcancelstate</B> returns the following error code on
  129. error:
  130. </P>
  131. <DL>
  132. <DL>
  133. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  134. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  135. the <I>state</I> argument is not
  136. </DD></DL>
  137. </DL>
  138. <BLOCKQUOTE>
  139. <B>PTHREAD_CANCEL_ENABLE</B> nor <B>PTHREAD_CANCEL_DISABLE</B>
  140. </BLOCKQUOTE>
  141. <P><B>pthread_setcanceltype</B> returns the following error code on
  142. error:
  143. </P>
  144. <DL>
  145. <DL>
  146. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  147. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  148. the <I>type</I> argument is not
  149. </DD></DL>
  150. </DL>
  151. <BLOCKQUOTE>
  152. <B>PTHREAD_CANCEL_DEFERRED</B> nor <B>PTHREAD_CANCEL_ASYNCHRONOUS</B>
  153. </BLOCKQUOTE>
  154. <H2><A HREF="#toc5" NAME="sect5">Author</A></H2>
  155. <P>Xavier Leroy &lt;[email protected]&gt;
  156. </P>
  157. <P>Modified by Ross Johnson for use with <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A>.</P>
  158. <H2><A HREF="#toc6" NAME="sect6">See Also</A></H2>
  159. <P><A HREF="pthread_exit.html"><B>pthread_exit</B>(3)</A> ,
  160. <A HREF="pthread_cleanup_push.html"><B>pthread_cleanup_push</B>(3)</A>
  161. , <A HREF="pthread_cleanup_pop.html"><B>pthread_cleanup_pop</B>(3)</A>
  162. , Pthreads-w32 package README file 'Prerequisites' section.
  163. </P>
  164. <H2><A HREF="#toc7" NAME="sect7">Bugs</A></H2>
  165. <P>POSIX specifies that a number of system calls (basically, all
  166. system calls that may block, such as <A HREF="read.html"><B>read</B>(2)</A>
  167. , <A HREF="write.html"><B>write</B>(2)</A> , <A HREF="wait.html"><B>wait</B>(2)</A>
  168. , etc.) and library functions that may call these system calls (e.g.
  169. <A HREF="fprintf.html"><B>fprintf</B>(3)</A> ) are cancellation
  170. points. <B>Pthreads-win32</B> is not integrated enough with the C
  171. library to implement this, and thus none of the C library functions
  172. is a cancellation point.
  173. </P>
  174. <P>A workaround for these calls is to temporarily switch to
  175. asynchronous cancellation (assuming full asynchronous cancellation
  176. support is installed). So, checking for cancellation during a <B>read</B>
  177. system call, for instance, can be achieved as follows:
  178. </P>
  179. <BLOCKQUOTE><BR><BR>
  180. </BLOCKQUOTE>
  181. <PRE STYLE="margin-left: 1cm; margin-right: 1cm">pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &amp;oldCancelType);
  182. read(fd, buffer, length);
  183. pthread_setcanceltype(oldCancelType, NULL);</PRE>
  184. <HR>
  185. <BLOCKQUOTE><A NAME="toc"></A><B>Table of Contents</B></BLOCKQUOTE>
  186. <UL>
  187. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect0" NAME="toc0">Name</A>
  188. </BLOCKQUOTE>
  189. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect1" NAME="toc1">Synopsis</A>
  190. </BLOCKQUOTE>
  191. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect2" NAME="toc2">Description</A>
  192. </BLOCKQUOTE>
  193. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect3" NAME="toc3">Return
  194. Value</A>
  195. </BLOCKQUOTE>
  196. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect4" NAME="toc4">Errors</A>
  197. </BLOCKQUOTE>
  198. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect5" NAME="toc5">Author</A>
  199. </BLOCKQUOTE>
  200. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect6" NAME="toc6">See
  201. Also</A>
  202. </BLOCKQUOTE>
  203. <LI><BLOCKQUOTE><A HREF="#sect7" NAME="toc7">Bugs</A>
  204. </BLOCKQUOTE>
  205. </UL>
  206. </BODY>
  207. </HTML>