⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pthread_mutexattr_destroy.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>pthread_mutexattr_destroy</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="pthread_mutexattr_destroy"></a> <a name="tag_03_544"></a><!-- pthread_mutexattr_destroy --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_544_01"></a>NAME</h4><blockquote>pthread_mutexattr_destroy, pthread_mutexattr_init - destroy and initialize the mutex attributes object</blockquote><h4><a name="tag_03_544_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('THR')">THR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include &lt;<a href="../basedefs/pthread.h.html">pthread.h</a>&gt;<br><br> int pthread_mutexattr_destroy(pthread_mutexattr_t *</tt><i>attr</i><tt>);<br> int pthread_mutexattr_init(pthread_mutexattr_t *</tt><i>attr</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_544_03"></a>DESCRIPTION</h4><blockquote><p>The <i>pthread_mutexattr_destroy</i>() function shall destroy a mutex attributes object; the object becomes, in effect,uninitialized. An implementation may cause <i>pthread_mutexattr_destroy</i>() to set the object referenced by <i>attr</i> to aninvalid value. A destroyed <i>attr</i> attributes object can be reinitialized using <i>pthread_mutexattr_init</i>(); the results ofotherwise referencing the object after it has been destroyed are undefined.</p><p>The <i>pthread_mutexattr_init</i>() function shall initialize a mutex attributes object <i>attr</i> with the default value forall of the attributes defined by the implementation.</p><p>Results are undefined if <i>pthread_mutexattr_init</i>() is called specifying an already initialized <i>attr</i> attributesobject.</p><p>After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object(including destruction) shall not affect any previously initialized mutexes.</p></blockquote><h4><a name="tag_03_544_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>pthread_mutexattr_destroy</i>() and <i>pthread_mutexattr_init</i>() shall return zero; otherwise,an error number shall be returned to indicate the error.</p></blockquote><h4><a name="tag_03_544_05"></a>ERRORS</h4><blockquote><p>The <i>pthread_mutexattr_destroy</i>() function may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The value specified by <i>attr</i> is invalid.</dd></dl><p>The <i>pthread_mutexattr_init</i>() function shall fail if:</p><dl compact><dt>[ENOMEM]</dt><dd>Insufficient memory exists to initialize the mutex attributes object.</dd></dl><p>These functions shall not return an error code of [EINTR].</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_544_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_544_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_544_08"></a>RATIONALE</h4><blockquote><p>See <a href="pthread_attr_init.html"><i>pthread_attr_init</i>()</a> for a general explanation of attributes. Attributes objectsallow implementations to experiment with useful extensions and permit extension of this volume of IEEE&nbsp;Std&nbsp;1003.1-2001without changing the existing functions. Thus, they provide for future extensibility of this volume ofIEEE&nbsp;Std&nbsp;1003.1-2001 and reduce the temptation to standardize prematurely on semantics that are not yet widelyimplemented or understood.</p><p>Examples of possible additional mutex attributes that have been discussed are <i>spin_only</i>, <i>limited_spin</i>,<i>no_spin</i>, <i>recursive</i>, and <i>metered</i>. (To explain what the latter attributes might mean: recursive mutexes wouldallow for multiple re-locking by the current owner; metered mutexes would transparently keep records of queue length, wait time,and so on.) Since there is not yet wide agreement on the usefulness of these resulting from shared implementation and usageexperience, they are not yet specified in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001. Mutex attributes objects, however, make itpossible to test out these concepts for possible standardization at a later time.</p><h5><a name="tag_03_544_08_01"></a>Mutex Attributes and Performance</h5><p>Care has been taken to ensure that the default values of the mutex attributes have been defined such that mutexes initializedwith the defaults have simple enough semantics so that the locking and unlocking can be done with the equivalent of a test-and-setinstruction (plus possibly a few other basic instructions).</p><p>There is at least one implementation method that can be used to reduce the cost of testing at lock-time if a mutex hasnon-default attributes. One such method that an implementation can employ (and this can be made fully transparent to fullyconforming POSIX applications) is to secretly pre-lock any mutexes that are initialized to non-default attributes. Any laterattempt to lock such a mutex causes the implementation to branch to the &quot;slow path&quot; as if the mutex were unavailable; then, onthe slow path, the implementation can do the &quot;real work&quot; to lock a non-default mutex. The underlying unlock operation is morecomplicated since the implementation never really wants to release the pre-lock on this kind of mutex. This illustrates that,depending on the hardware, there may be certain optimizations that can be used so that whatever mutex attributes are considered&quot;most frequently used&quot; can be processed most efficiently.</p><h5><a name="tag_03_544_08_02"></a>Process Shared Memory and Synchronization</h5><p>The existence of memory mapping functions in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 leads to the possibility that anapplication may allocate the synchronization objects from this section in memory that is accessed by multiple processes (andtherefore, by threads of multiple processes).</p><p>In order to permit such usage, while at the same time keeping the usual case (that is, usage within a single process) efficient,a <i>process-shared</i> option has been defined.</p><p>If an implementation supports the _POSIX_THREAD_PROCESS_SHARED option, then the <i>process-shared</i> attribute can be used toindicate that mutexes or condition variables may be accessed by threads of multiple processes.</p><p>The default setting of PTHREAD_PROCESS_PRIVATE has been chosen for the <i>process-shared</i> attribute so that the mostefficient forms of these synchronization objects are created by default.</p><p>Synchronization variables that are initialized with the PTHREAD_PROCESS_PRIVATE <i>process-shared</i> attribute may only beoperated on by threads in the process that initialized them. Synchronization variables that are initialized with thePTHREAD_PROCESS_SHARED <i>process-shared</i> attribute may be operated on by any thread in any process that has access to it. Inparticular, these processes may exist beyond the lifetime of the initializing process. For example, the following code implements asimple counting semaphore in a mapped file that may be used by many processes.</p><pre><tt>/* sem.h */struct semaphore {    pthread_mutex_t lock;    pthread_cond_t nonzero;    unsigned count;};typedef struct semaphore semaphore_t;<br>semaphore_t *semaphore_create(char *semaphore_name);semaphore_t *semaphore_open(char *semaphore_name);void semaphore_post(semaphore_t *semap);void semaphore_wait(semaphore_t *semap);void semaphore_close(semaphore_t *semap);<br>/* sem.c */#include &lt;sys/types.h&gt;#include &lt;sys/stat.h&gt;#include &lt;sys/mman.h&gt;#include &lt;fcntl.h&gt;#include &lt;pthread.h&gt;#include "sem.h"<br>semaphore_t *semaphore_create(char *semaphore_name){int fd;    semaphore_t *semap;    pthread_mutexattr_t psharedm;    pthread_condattr_t psharedc;<br>    fd = open(semaphore_name, O_RDWR | O_CREAT | O_EXCL, 0666);    if (fd &lt; 0)        return (NULL);    (void) ftruncate(fd, sizeof(semaphore_t));    (void) pthread_mutexattr_init(&amp;psharedm);    (void) pthread_mutexattr_setpshared(&amp;psharedm,        PTHREAD_PROCESS_SHARED);    (void) pthread_condattr_init(&amp;psharedc);    (void) pthread_condattr_setpshared(&amp;psharedc,        PTHREAD_PROCESS_SHARED);    semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),            PROT_READ | PROT_WRITE, MAP_SHARED,            fd, 0);    close (fd);    (void) pthread_mutex_init(&amp;semap-&gt;lock, &amp;psharedm);    (void) pthread_cond_init(&amp;semap-&gt;nonzero, &amp;psharedc);    semap-&gt;count = 0;    return (semap);}<br>semaphore_t *semaphore_open(char *semaphore_name){    int fd;    semaphore_t *semap;<br>    fd = open(semaphore_name, O_RDWR, 0666);    if (fd &lt; 0)        return (NULL);    semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),            PROT_READ | PROT_WRITE, MAP_SHARED,            fd, 0);    close (fd);    return (semap);}<br>voidsemaphore_post(semaphore_t *semap){    pthread_mutex_lock(&amp;semap-&gt;lock);    if (semap-&gt;count == 0)        pthread_cond_signal(&amp;semapx-&gt;nonzero);    semap-&gt;count++;    pthread_mutex_unlock(&amp;semap-&gt;lock);}<br>voidsemaphore_wait(semaphore_t *semap){    pthread_mutex_lock(&amp;semap-&gt;lock);    while (semap-&gt;count == 0)        pthread_cond_wait(&amp;semap-&gt;nonzero, &amp;semap-&gt;lock);    semap-&gt;count--;    pthread_mutex_unlock(&amp;semap-&gt;lock);}<br>voidsemaphore_close(semaphore_t *semap){    munmap((void *) semap, sizeof(semaphore_t));}</tt></pre><p>The following code is for three separate processes that create, post, and wait on a semaphore in the file <b>/tmp/semaphore</b>.Once the file is created, the post and wait programs increment and decrement the counting semaphore (waiting and waking asrequired) even though they did not initialize the semaphore.</p><pre><tt>/* create.c */#include "pthread.h"#include "sem.h"<br>intmain(){    semaphore_t *semap;<br>    semap = semaphore_create("/tmp/semaphore");    if (semap == NULL)        exit(1);    semaphore_close(semap);    return (0);}<br>/* post */#include "pthread.h"#include "sem.h"<br>intmain(){    semaphore_t *semap;<br>    semap = semaphore_open("/tmp/semaphore");    if (semap == NULL)        exit(1);    semaphore_post(semap);    semaphore_close(semap);    return (0);}<br>/* wait */#include "pthread.h"#include "sem.h"<br>intmain(){    semaphore_t *semap;<br>    semap = semaphore_open("/tmp/semaphore");    if (semap == NULL)        exit(1);    semaphore_wait(semap);    semaphore_close(semap);    return (0);}</tt></pre></blockquote><h4><a name="tag_03_544_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_544_10"></a>SEE ALSO</h4><blockquote><p><a href="pthread_cond_destroy.html"><i>pthread_cond_destroy</i>()</a> , <a href="pthread_create.html"><i>pthread_create</i>()</a> , <a href="pthread_mutex_destroy.html"><i>pthread_mutex_destroy</i>()</a> , <ahref="#">pthread_mutexattr_destroy</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/pthread.h.html"><i>&lt;pthread.h&gt;</i></a></p></blockquote><h4><a name="tag_03_544_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 5. Included for alignment with the POSIX Threads Extension.</p></blockquote><h4><a name="tag_03_544_12"></a>Issue 6</h4><blockquote><p>The <i>pthread_mutexattr_destroy</i>() and <i>pthread_mutexattr_init</i>() functions are marked as part of the Threadsoption.</p><p>IEEE PASC Interpretation 1003.1c #27 is applied, updating the ERRORS section.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -