add directory Ref-docs
This commit is contained in:
351
Ref-docs/POSIX/susv3/functions/pthread_mutexattr_destroy.html
Normal file
351
Ref-docs/POSIX/susv3/functions/pthread_mutexattr_destroy.html
Normal file
@@ -0,0 +1,351 @@
|
||||
<!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 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-2001<br>
|
||||
Copyright © 2001 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 <<a href="../basedefs/pthread.h.html">pthread.h</a>><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 an
|
||||
invalid value. A destroyed <i>attr</i> attributes object can be reinitialized using <i>pthread_mutexattr_init</i>(); the results of
|
||||
otherwise 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 for
|
||||
all 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> attributes
|
||||
object.</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 objects
|
||||
allow implementations to experiment with useful extensions and permit extension of this volume of IEEE Std 1003.1-2001
|
||||
without changing the existing functions. Thus, they provide for future extensibility of this volume of
|
||||
IEEE Std 1003.1-2001 and reduce the temptation to standardize prematurely on semantics that are not yet widely
|
||||
implemented 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 would
|
||||
allow 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 usage
|
||||
experience, they are not yet specified in this volume of IEEE Std 1003.1-2001. Mutex attributes objects, however, make it
|
||||
possible 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 initialized
|
||||
with the defaults have simple enough semantics so that the locking and unlocking can be done with the equivalent of a test-and-set
|
||||
instruction (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 has
|
||||
non-default attributes. One such method that an implementation can employ (and this can be made fully transparent to fully
|
||||
conforming POSIX applications) is to secretly pre-lock any mutexes that are initialized to non-default attributes. Any later
|
||||
attempt to lock such a mutex causes the implementation to branch to the "slow path" as if the mutex were unavailable; then, on
|
||||
the slow path, the implementation can do the "real work" to lock a non-default mutex. The underlying unlock operation is more
|
||||
complicated 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
|
||||
"most frequently used" 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 Std 1003.1-2001 leads to the possibility that an
|
||||
application may allocate the synchronization objects from this section in memory that is accessed by multiple processes (and
|
||||
therefore, 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 to
|
||||
indicate 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 most
|
||||
efficient 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 be
|
||||
operated on by threads in the process that initialized them. Synchronization variables that are initialized with the
|
||||
PTHREAD_PROCESS_SHARED <i>process-shared</i> attribute may be operated on by any thread in any process that has access to it. In
|
||||
particular, these processes may exist beyond the lifetime of the initializing process. For example, the following code implements a
|
||||
simple 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 <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#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 < 0)
|
||||
return (NULL);
|
||||
(void) ftruncate(fd, sizeof(semaphore_t));
|
||||
(void) pthread_mutexattr_init(&psharedm);
|
||||
(void) pthread_mutexattr_setpshared(&psharedm,
|
||||
PTHREAD_PROCESS_SHARED);
|
||||
(void) pthread_condattr_init(&psharedc);
|
||||
(void) pthread_condattr_setpshared(&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(&semap->lock, &psharedm);
|
||||
(void) pthread_cond_init(&semap->nonzero, &psharedc);
|
||||
semap->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 < 0)
|
||||
return (NULL);
|
||||
semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),
|
||||
PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||
fd, 0);
|
||||
close (fd);
|
||||
return (semap);
|
||||
}
|
||||
<br>
|
||||
void
|
||||
semaphore_post(semaphore_t *semap)
|
||||
{
|
||||
pthread_mutex_lock(&semap->lock);
|
||||
if (semap->count == 0)
|
||||
pthread_cond_signal(&semapx->nonzero);
|
||||
semap->count++;
|
||||
pthread_mutex_unlock(&semap->lock);
|
||||
}
|
||||
<br>
|
||||
void
|
||||
semaphore_wait(semaphore_t *semap)
|
||||
{
|
||||
pthread_mutex_lock(&semap->lock);
|
||||
while (semap->count == 0)
|
||||
pthread_cond_wait(&semap->nonzero, &semap->lock);
|
||||
semap->count--;
|
||||
pthread_mutex_unlock(&semap->lock);
|
||||
}
|
||||
<br>
|
||||
void
|
||||
semaphore_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 as
|
||||
required) even though they did not initialize the semaphore.</p>
|
||||
|
||||
<pre>
|
||||
<tt>/* create.c */
|
||||
#include "pthread.h"
|
||||
#include "sem.h"
|
||||
<br>
|
||||
int
|
||||
main()
|
||||
{
|
||||
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>
|
||||
int
|
||||
main()
|
||||
{
|
||||
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>
|
||||
int
|
||||
main()
|
||||
{
|
||||
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> , <a
|
||||
href="#">pthread_mutexattr_destroy</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
||||
"../basedefs/pthread.h.html"><i><pthread.h></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 Threads
|
||||
option.</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 ® is a registered Trademark of The Open Group.<br>
|
||||
POSIX ® 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>
|
||||
|
||||
Reference in New Issue
Block a user