pthread_key_create.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_SPECIFIC(3) manual page</TITLE>
  6. <META NAME="GENERATOR" CONTENT="OpenOffice.org 1.1.3 (Linux)">
  7. <META NAME="CREATED" CONTENT="20050504;18425400">
  8. <META NAME="CHANGED" CONTENT="20050509;18220200">
  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_key_create, pthread_key_delete, pthread_setspecific,
  18. pthread_getspecific - management of thread-specific data
  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_key_create(pthread_key_t *</B><I>key</I><B>, void
  24. (*</B><I>destr_function</I><B>) (void *));</B>
  25. </P>
  26. <P><B>int pthread_key_delete(pthread_key_t </B><I>key</I><B>);</B>
  27. </P>
  28. <P><B>int pthread_setspecific(pthread_key_t </B><I>key</I><B>, const
  29. void *</B><I>pointer</I><B>);</B>
  30. </P>
  31. <P><B>void * pthread_getspecific(pthread_key_t </B><I>key</I><B>);</B>
  32. </P>
  33. <H2><A HREF="#toc2" NAME="sect2">Description</A></H2>
  34. <P>Programs often need global or static variables that have different
  35. values in different threads. Since threads share one memory space,
  36. this cannot be achieved with regular variables. Thread-specific data
  37. is the POSIX threads answer to this need.
  38. </P>
  39. <P>Each thread possesses a private memory block, the thread-specific
  40. data area, or TSD area for short. This area is indexed by TSD keys.
  41. The TSD area associates values of type <B>void *</B> to TSD keys. TSD
  42. keys are common to all threads, but the value associated with a given
  43. TSD key can be different in each thread.
  44. </P>
  45. <P>For concreteness, the TSD areas can be viewed as arrays of <B>void
  46. *</B> pointers, TSD keys as integer indices into these arrays, and
  47. the value of a TSD key as the value of the corresponding array
  48. element in the calling thread.
  49. </P>
  50. <P>When a thread is created, its TSD area initially associates <B>NULL</B>
  51. with all keys.
  52. </P>
  53. <P><B>pthread_key_create</B> allocates a new TSD key. The key is
  54. stored in the location pointed to by <I>key</I>. There is a limit of
  55. <B>PTHREAD_KEYS_MAX</B> on the number of keys allocated at a given
  56. time. The value initially associated with the returned key is <B>NULL</B>
  57. in all currently executing threads.
  58. </P>
  59. <P>The <I>destr_function</I> argument, if not <B>NULL</B>, specifies
  60. a destructor function associated with the key. When a thread
  61. terminates via <B>pthread_exit</B> or by cancellation, <I>destr_function</I>
  62. is called with arguments the value associated with the key in that
  63. thread. The <I>destr_function</I> is not called if that value is <B>NULL</B><SPAN STYLE="font-weight: medium">
  64. or the key has been deleted</SPAN>. The order in which destructor
  65. functions are called at thread termination time is unspecified.
  66. </P>
  67. <P>Before the destructor function is called, the <B>NULL</B> value is
  68. associated with the key in the current thread. A destructor function
  69. might, however, re-associate non- <B>NULL</B> values to that key or
  70. some other key. To deal with this, if after all the destructors have
  71. been called for all non- <B>NULL</B> values, there are still some
  72. non- <B>NULL</B> values with associated destructors, then the process
  73. is repeated.</P>
  74. <P><B>pthread_key_delete</B> deallocates a TSD key. It does not check
  75. whether non- <B>NULL</B> values are associated with that key in the
  76. currently executing threads, nor call the destructor function
  77. associated with the key.
  78. </P>
  79. <P><B>pthread_setspecific</B> changes the value associated with <I>key</I>
  80. in the calling thread, storing the given <I>pointer</I> instead.
  81. </P>
  82. <P><B>pthread_getspecific</B> returns the value currently associated
  83. with <I>key</I> in the calling thread.
  84. </P>
  85. <P>The routines <B>pthread_setspecific</B>, <B>pthread_getspecific</B>,
  86. and <B>pthread_key_delete</B> can be called from <I>destr_function</I>
  87. targeting any valid key including the key on which <I>destr_function</I>
  88. is currently operating. If <B>pthread_getspecific</B> is called on
  89. the key whose thread specific data is being destroyed, the value NULL
  90. is returned, unless <B>pthread_setspecific</B> was called previously
  91. on that key from within <I>destr_function</I> to set the value to
  92. non-NULL. For some implementations the effect of calling
  93. <B>pthread_setspecific</B> from within <I>destr_function</I> can be
  94. either memory leakage or infinite loops if <I>destr_function</I> has
  95. already been called at least <B>PTHREAD_DESTRUCTOR_ITERATIONS</B>
  96. times.</P>
  97. <P STYLE="font-weight: medium"><B>Pthreads-w32</B> stops running key
  98. <I>destr_function</I> routines after <B>PTHREAD_DESTRUCTOR_ITERATIONS</B>
  99. iterations, even if some non- <B>NULL</B> values with associated
  100. descriptors remain. If memory is allocated and associated with a key
  101. from within <I>destr_function</I>, that memory may not be reclaimed
  102. because that key's <I>destr_function</I>, may not run again.</P>
  103. <H2><A HREF="#toc3" NAME="sect3">Return Value</A></H2>
  104. <P><B>pthread_key_create</B>, <B>pthread_key_delete</B>, and
  105. <B>pthread_setspecific</B> return 0 on success and a non-zero error
  106. code on failure. If successful, <B>pthread_key_create</B> stores the
  107. newly allocated key in the location pointed to by its <I>key</I>
  108. argument.
  109. </P>
  110. <P><B>pthread_getspecific</B> returns the value associated with <I>key</I>
  111. on success, and <B>NULL</B> on error.
  112. </P>
  113. <H2><A HREF="#toc4" NAME="sect4">Errors</A></H2>
  114. <P><B>pthread_key_create</B> returns the following error code on
  115. error:
  116. </P>
  117. <DL>
  118. <DL>
  119. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EAGAIN</B>
  120. </DT></DL>
  121. </DL>
  122. <BLOCKQUOTE STYLE="margin-left: 5cm">
  123. <B>PTHREAD_KEYS_MAX</B> keys are already allocated
  124. </BLOCKQUOTE>
  125. <DL>
  126. <DL>
  127. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>ENOMEM</B>
  128. </DT></DL>
  129. </DL>
  130. <BLOCKQUOTE STYLE="margin-left: 5cm">
  131. Insufficient memory to allocate the key.
  132. </BLOCKQUOTE>
  133. <P><B>pthread_key_delete</B> and <B>pthread_setspecific</B> return
  134. the following error code on error:
  135. </P>
  136. <DL>
  137. <DL>
  138. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  139. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  140. <I>key</I> is not a valid, allocated TSD key
  141. </DD></DL>
  142. </DL>
  143. <P>
  144. <B>pthread_getspecific</B> returns <B>NULL</B> if <I>key</I> is not a
  145. valid, allocated TSD key.
  146. </P>
  147. <H2><A HREF="#toc5" NAME="sect5">Author</A></H2>
  148. <P>Xavier Leroy &lt;[email protected]&gt;
  149. </P>
  150. <P>Modified by Ross Johnson for use with <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A>.</P>
  151. <H2><A HREF="#toc6" NAME="sect6">See Also</A></H2>
  152. <P><A HREF="pthread_create.html">pthread_create(3)</A> ,
  153. <A HREF="pthread_exit.html">pthread_exit(3)</A> ,
  154. <A HREF="pthread_cancel.html">pthread_testcancel(3)</A> .
  155. </P>
  156. <H2><A HREF="#toc7" NAME="sect7">Example</A></H2>
  157. <P>The following code fragment allocates a thread-specific array of
  158. 100 characters, with automatic reclamation at thread exit:
  159. </P>
  160. <BLOCKQUOTE><BR><BR>
  161. </BLOCKQUOTE>
  162. <PRE STYLE="margin-left: 1cm; margin-right: 1cm">/* Key for the thread-specific buffer */
  163. static pthread_key_t buffer_key;
  164. /* Once-only initialisation of the key */
  165. static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
  166. /* Allocate the thread-specific buffer */
  167. void buffer_alloc(void)
  168. {
  169. pthread_once(&amp;buffer_key_once, buffer_key_alloc);
  170. pthread_setspecific(buffer_key, malloc(100));
  171. }
  172. /* Return the thread-specific buffer */
  173. char * get_buffer(void)
  174. {
  175. return (char *) pthread_getspecific(buffer_key);
  176. }
  177. /* Allocate the key */
  178. static void buffer_key_alloc()
  179. {
  180. pthread_key_create(&amp;buffer_key, buffer_destroy);
  181. }
  182. /* Free the thread-specific buffer */
  183. static void buffer_destroy(void * buf)
  184. {
  185. free(buf);
  186. }</PRE>
  187. <HR>
  188. <BLOCKQUOTE STYLE="margin-left: 0cm; margin-right: 0cm"><A NAME="toc"></A>
  189. <B>Table of Contents</B></BLOCKQUOTE>
  190. <UL>
  191. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect0" NAME="toc0">Name</A>
  192. </BLOCKQUOTE>
  193. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect1" NAME="toc1">Synopsis</A>
  194. </BLOCKQUOTE>
  195. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect2" NAME="toc2">Description</A>
  196. </BLOCKQUOTE>
  197. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect3" NAME="toc3">Return
  198. Value</A>
  199. </BLOCKQUOTE>
  200. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect4" NAME="toc4">Errors</A>
  201. </BLOCKQUOTE>
  202. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect5" NAME="toc5">Author</A>
  203. </BLOCKQUOTE>
  204. <LI><BLOCKQUOTE STYLE="margin-bottom: 0cm"><A HREF="#sect6" NAME="toc6">See
  205. Also</A>
  206. </BLOCKQUOTE>
  207. <LI><BLOCKQUOTE><A HREF="#sect7" NAME="toc7">Example</A>
  208. </BLOCKQUOTE>
  209. </UL>
  210. </BODY>
  211. </HTML>