pthread_mutexattr_init.html 11 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_MUTEXATTR(3) manual page</TITLE>
  6. <META NAME="GENERATOR" CONTENT="OpenOffice.org 3.2 (Linux)">
  7. <META NAME="CREATED" CONTENT="20050504;23040500">
  8. <META NAME="CHANGEDBY" CONTENT="Ross Johnson">
  9. <META NAME="CHANGED" CONTENT="20110326;13190500">
  10. <!-- manual page source format generated by PolyglotMan v3.2, -->
  11. <!-- available at http://polyglotman.sourceforge.net/ -->
  12. <STYLE TYPE="text/css">
  13. <!--
  14. H4.cjk { font-family: "AR PL UMing CN" }
  15. H4.ctl { font-family: "Lohit Devanagari" }
  16. H2.cjk { font-family: "AR PL UMing CN" }
  17. H2.ctl { font-family: "Lohit Devanagari" }
  18. -->
  19. </STYLE>
  20. </HEAD>
  21. <BODY LANG="en-GB" BGCOLOR="#ffffff" DIR="LTR">
  22. <H4 CLASS="western">POSIX Threads for Windows – REFERENCE -
  23. <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A></H4>
  24. <P><A HREF="index.html">Reference Index</A></P>
  25. <P><A HREF="#toc">Table of Contents</A></P>
  26. <H2 CLASS="western"><A HREF="#toc0" NAME="sect0">Name</A></H2>
  27. <P>pthread_mutexattr_init, pthread_mutexattr_destroy,
  28. pthread_mutexattr_settype, pthread_mutexattr_gettype - mutex creation
  29. attributes
  30. </P>
  31. <H2 CLASS="western"><A HREF="#toc1" NAME="sect1">Synopsis</A></H2>
  32. <P><B>#include &lt;pthread.h&gt;</B>
  33. </P>
  34. <P><B>int pthread_mutexattr_init(pthread_mutexattr_t *</B><I>attr</I><B>);</B>
  35. </P>
  36. <P><B>int pthread_mutexattr_destroy(pthread_mutexattr_t *</B><I>attr</I><B>);</B>
  37. </P>
  38. <P><B>int pthread_mutexattr_settype(pthread_mutexattr_t *</B><I>attr</I><B>,
  39. int </B><I>type</I><B>);</B>
  40. </P>
  41. <P><B>int pthread_mutexattr_gettype(const pthread_mutexattr_t *</B><I>attr</I><B>,
  42. int *</B><I>type</I><B>);</B>
  43. </P>
  44. <P><B>int pthread_mutexattr_setkind_np(pthread_mutexattr_t *</B><I>attr</I><B>,
  45. int </B><I>type</I><B>);</B>
  46. </P>
  47. <P><B>int pthread_mutexattr_getkind_np(const pthread_mutexattr_t
  48. *</B><I>attr</I><B>, int *</B><I>type</I><B>);</B>
  49. </P>
  50. <P><B>int pthread_mutexattr_setrobust(pthread_mutexattr_t *</B><I>attr</I><B>,
  51. int</B><SPAN STYLE="font-weight: normal"> </SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><B>);</B>
  52. </P>
  53. <P><B>int pthread_mutexattr_getrobust(pthread_mutexattr_t *</B><I>attr</I><B>,
  54. int</B><SPAN STYLE="font-weight: normal"> </SPAN><B>*</B><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><B>);</B>
  55. </P>
  56. <H2 CLASS="western"><A HREF="#toc2" NAME="sect2">Description</A></H2>
  57. <P>Mutex attributes can be specified at mutex creation time, by
  58. passing a mutex attribute object as second argument to
  59. <A HREF="pthread_mutex_init.html"><B>pthread_mutex_init</B>(3)</A> .
  60. Passing <B>NULL</B> is equivalent to passing a mutex attribute object
  61. with all attributes set to their default values.
  62. </P>
  63. <P><B>pthread_mutexattr_init</B> initializes the mutex attribute
  64. object <I>attr</I> and fills it with default values for the
  65. attributes.
  66. </P>
  67. <P><B>pthread_mutexattr_destroy</B> destroys a mutex attribute
  68. object, which must not be reused until it is reinitialized.</P>
  69. <P><B>pthread_mutexattr_settype</B> sets the mutex type attribute in
  70. <I>attr</I> to the value specified by <I>type</I>.
  71. </P>
  72. <P><B>pthread_mutexattr_gettype</B> retrieves the current value of
  73. the mutex kind attribute in <I>attr</I> and stores it in the location
  74. pointed to by <I>type</I>.
  75. </P>
  76. <P><B>Pthreads-w32</B> also recognises the following equivalent
  77. functions that are used in Linux:</P>
  78. <P><B>pthread_mutexattr_setkind_np</B> is an alias for
  79. <B>pthread_mutexattr_settype</B>.
  80. </P>
  81. <P STYLE="font-weight: normal"><B>pthread_mutexattr_getkind_np</B> is
  82. an alias for <B>pthread_mutexattr_gettype</B>.
  83. </P>
  84. <P>The following mutex types are supported:</P>
  85. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_NORMAL</B> - for
  86. ‘‘fast’’ mutexes.</P>
  87. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_RECURSIVE</B> - for
  88. ‘‘recursive’’ mutexes.</P>
  89. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_ERRORCHECK</B> - for
  90. ‘‘error checking’’ mutexes.</P>
  91. <P>The mutex type determines what happens if a thread attempts to
  92. lock a mutex it already owns with <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  93. . If the mutex is of the “normal” or “fast” type,
  94. <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  95. simply suspends the calling thread forever. If the mutex is of the
  96. ‘‘error checking’’ type, <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  97. returns immediately with the error code <B>EDEADLK</B>. If the mutex
  98. is of the ‘‘recursive’’ type, the call to
  99. <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  100. returns immediately with a success return code. The number of times
  101. the thread owning the mutex has locked it is recorded in the mutex.
  102. The owning thread must call <A HREF="pthread_mutex_unlock.html"><B>pthread_mutex_unlock</B>(3)</A>
  103. the same number of times before the mutex returns to the unlocked
  104. state.
  105. </P>
  106. <P>The default mutex type is <B>PTHREAD_MUTEX_NORMAL</B></P>
  107. <P><B>Pthreads-w32</B> also recognises the following equivalent types
  108. that are used by Linux:</P>
  109. <P STYLE="margin-left: 0.79in; font-weight: normal"><B>PTHREAD_MUTEX_FAST_NP</B>
  110. – equivalent to <B>PTHREAD_MUTEX_NORMAL</B></P>
  111. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_RECURSIVE_NP</B></P>
  112. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_ERRORCHECK_NP</B></P>
  113. <P><B>pthread_mutexattr_setrobust</B><SPAN STYLE="font-weight: normal">
  114. sets the robustness attribute to the value given by </SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><SPAN STYLE="font-weight: normal">.</SPAN></P>
  115. <P><B>pthread_mutexattr_getrobust</B><SPAN STYLE="font-weight: normal">
  116. returns the current robustness value to the location given by
  117. *</SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><SPAN STYLE="font-weight: normal">.</SPAN></P>
  118. <P><SPAN STYLE="font-weight: normal">The possible values for </SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><SPAN STYLE="font-weight: normal">
  119. are:</SPAN></P>
  120. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_STALLED</B><SPAN STYLE="font-weight: normal">
  121. - when the owner of the mutex terminates without unlocking the mutex,
  122. all subsequent calls to pthread_mutex_*lock() are blocked from
  123. progress in an unspecified manner.</SPAN></P>
  124. <P STYLE="margin-left: 0.79in"><B>PTHREAD_MUTEX_ROBUST</B><SPAN STYLE="font-weight: normal">
  125. - when the owner of the mutex terminates without unlocking the mutex,
  126. the mutex is unlocked. The next owner of this mutex acquires the
  127. mutex with an error return of EOWNERDEAD.</SPAN></P>
  128. <H2 CLASS="western"><A HREF="#toc3" NAME="sect3">Return Value</A></H2>
  129. <P><SPAN STYLE="font-weight: normal">On success all functions return
  130. 0, otherwise they return an error code as follows:</SPAN></P>
  131. <P><B>pthread_mutexattr_init</B></P>
  132. <P STYLE="margin-left: 0.79in"><B>ENOMEM</B><SPAN STYLE="font-weight: normal">
  133. - insufficient memory for </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">.</SPAN></P>
  134. <P><B>pthread_mutexattr_destroy</B></P>
  135. <P STYLE="margin-left: 0.79in"><B>EINVAL</B><SPAN STYLE="font-weight: normal">
  136. - </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">
  137. is invalid.</SPAN></P>
  138. <P><B>pthread_mutexattr_gettype</B></P>
  139. <P STYLE="margin-left: 0.79in"><B>EINVAL</B><SPAN STYLE="font-weight: normal">
  140. - </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">
  141. is invalid.</SPAN></P>
  142. <P><B>pthread_mutexattr_settype</B></P>
  143. <DL>
  144. <DL>
  145. <DL>
  146. <DT><B>EINVAL</B><SPAN STYLE="font-weight: normal"> - </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">
  147. is invalid or </SPAN><I><SPAN STYLE="font-weight: normal">type</SPAN></I><SPAN STYLE="font-weight: normal">
  148. is none of:</SPAN></DT><DL>
  149. <DL>
  150. <DT>
  151. <B>PTHREAD_MUTEX_NORMAL<BR>PTHREAD_MUTEX_FAST_NP<BR>PTHREAD_MUTEX_RECURSIVE<BR>PTHREAD_MUTEX_RECURSIVE_NP<BR>PTHREAD_MUTEX_ERRORCHECK<BR>PTHREAD_MUTEX_ERRORCHECK_NP</B></DT></DL>
  152. </DL>
  153. </DL>
  154. </DL>
  155. <DD STYLE="margin-left: 0in">
  156. <BR>
  157. </DD></DL>
  158. <P>
  159. <B>pthread_mutexattr_getrobust</B></P>
  160. <P STYLE="margin-left: 0.79in"><B>EINVAL</B><SPAN STYLE="font-weight: normal">
  161. – </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">
  162. or </SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><SPAN STYLE="font-weight: normal">
  163. is invalid.</SPAN></P>
  164. <P><B>pthread_mutexattr_setrobust</B></P>
  165. <P STYLE="margin-left: 0.79in"><B>EINVAL</B><SPAN STYLE="font-weight: normal">
  166. – </SPAN><I><SPAN STYLE="font-weight: normal">attr</SPAN></I><SPAN STYLE="font-weight: normal">
  167. or </SPAN><I><SPAN STYLE="font-weight: normal">robust</SPAN></I><SPAN STYLE="font-weight: normal">
  168. is invalid.</SPAN></P>
  169. <H2 CLASS="western"><A HREF="#toc5" NAME="sect5">Author</A></H2>
  170. <P>Xavier Leroy &lt;[email protected]&gt;
  171. </P>
  172. <P>Modified by Ross Johnson for use with <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A>.</P>
  173. <H2 CLASS="western"><A HREF="#toc6" NAME="sect6">See Also</A></H2>
  174. <P><A HREF="pthread_mutex_init.html"><B>pthread_mutex_init</B>(3)</A>
  175. , <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  176. , <A HREF="pthread_mutex_unlock.html"><B>pthread_mutex_unlock</B>(3)</A>
  177. .
  178. </P>
  179. <H2 CLASS="western"><A HREF="#toc7" NAME="sect7"><FONT COLOR="#000080"><U>Notes</U></FONT></A></H2>
  180. <P>For speed, <B>Pthreads-w32</B> never checks the thread ownership
  181. of non-robust mutexes of type <B>PTHREAD_MUTEX_NORMAL</B> (or
  182. <B>PTHREAD_MUTEX_FAST_NP</B>) when performing operations on the
  183. mutex. It is therefore possible for one thread to lock such a mutex
  184. and another to unlock it.</P>
  185. <P STYLE="font-weight: normal">When developing code, it is a common
  186. precaution to substitute the error checking type, then drop in the
  187. normal type for release if the extra performance is required.</P>
  188. <HR>
  189. <P><A NAME="toc"></A><B>Table of Contents</B></P>
  190. <UL>
  191. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect0" NAME="toc0">Name</A>
  192. </P>
  193. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect1" NAME="toc1">Synopsis</A>
  194. </P>
  195. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect2" NAME="toc2">Description</A>
  196. </P>
  197. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect3" NAME="toc3">Return
  198. Value</A>
  199. </P>
  200. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect5" NAME="toc5">Author</A>
  201. </P>
  202. <LI><P STYLE="margin-bottom: 0in"><A HREF="#sect6" NAME="toc6">See
  203. Also</A></P>
  204. <LI><P><A HREF="#sect7" NAME="toc7">Notes</A></P>
  205. </UL>
  206. </BODY>
  207. </HTML>