241 lines
9.1 KiB
HTML
241 lines
9.1 KiB
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 The Open Group, All Rights Reserved -->
|
|
<title>pthread_cond_destroy</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="pthread_cond_destroy"></a> <a name="tag_03_516"></a><!-- pthread_cond_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_516_01"></a>NAME</h4>
|
|
|
|
<blockquote>pthread_cond_destroy, pthread_cond_init - destroy and initialize condition variables</blockquote>
|
|
|
|
<h4><a name="tag_03_516_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_cond_destroy(pthread_cond_t *</tt><i>cond</i><tt>);<br>
|
|
int pthread_cond_init(pthread_cond_t *restrict</tt> <i>cond</i><tt>,<br>
|
|
const pthread_condattr_t *restrict</tt> <i>attr</i><tt>);<br>
|
|
pthread_cond_t</tt> <i>cond</i> <tt>= PTHREAD_COND_INITIALIZER; <img src="../images/opt-end.gif" alt="[Option End]" border=
|
|
"0"></tt></code></div>
|
|
|
|
<tt><br>
|
|
</tt></blockquote>
|
|
|
|
<h4><a name="tag_03_516_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>pthread_cond_destroy</i>() function shall destroy the given condition variable specified by <i>cond</i>; the object
|
|
becomes, in effect, uninitialized. An implementation may cause <i>pthread_cond_destroy</i>() to set the object referenced by
|
|
<i>cond</i> to an invalid value. A destroyed condition variable object can be reinitialized using <i>pthread_cond_init</i>(); the
|
|
results of otherwise referencing the object after it has been destroyed are undefined.</p>
|
|
|
|
<p>It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked. Attempting to destroy
|
|
a condition variable upon which other threads are currently blocked results in undefined behavior.</p>
|
|
|
|
<p>The <i>pthread_cond_init</i>() function shall initialize the condition variable referenced by <i>cond</i> with attributes
|
|
referenced by <i>attr</i>. If <i>attr</i> is NULL, the default condition variable attributes shall be used; the effect is the same
|
|
as passing the address of a default condition variable attributes object. Upon successful initialization, the state of the
|
|
condition variable shall become initialized.</p>
|
|
|
|
<p>Only <i>cond</i> itself may be used for performing synchronization. The result of referring to copies of <i>cond</i> in calls to
|
|
<a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a>, <a href=
|
|
"../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>, <a href=
|
|
"../functions/pthread_cond_signal.html"><i>pthread_cond_signal</i>()</a>, <a href=
|
|
"../functions/pthread_cond_broadcast.html"><i>pthread_cond_broadcast</i>()</a>, and <i>pthread_cond_destroy</i>() is undefined.</p>
|
|
|
|
<p>Attempting to initialize an already initialized condition variable results in undefined behavior.</p>
|
|
|
|
<p>In cases where default condition variable attributes are appropriate, the macro PTHREAD_COND_INITIALIZER can be used to
|
|
initialize condition variables that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to
|
|
<i>pthread_cond_init</i>() with parameter <i>attr</i> specified as NULL, except that no error checks are performed.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>If successful, the <i>pthread_cond_destroy</i>() and <i>pthread_cond_init</i>() functions shall return zero; otherwise, an error
|
|
number shall be returned to indicate the error.</p>
|
|
|
|
<p>The [EBUSY] and [EINVAL] error checks, if implemented, shall act as if they were performed immediately at the beginning of
|
|
processing for the function and caused an error return prior to modifying the state of the condition variable specified by
|
|
<i>cond</i>.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>pthread_cond_destroy</i>() function may fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EBUSY]</dt>
|
|
|
|
<dd>The implementation has detected an attempt to destroy the object referenced by <i>cond</i> while it is referenced (for example,
|
|
while being used in a <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> or <a href=
|
|
"../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>) by another thread.</dd>
|
|
|
|
<dt>[EINVAL]</dt>
|
|
|
|
<dd>The value specified by <i>cond</i> is invalid.</dd>
|
|
</dl>
|
|
|
|
<p>The <i>pthread_cond_init</i>() function shall fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EAGAIN]</dt>
|
|
|
|
<dd>The system lacked the necessary resources (other than memory) to initialize another condition variable.</dd>
|
|
|
|
<dt>[ENOMEM]</dt>
|
|
|
|
<dd>Insufficient memory exists to initialize the condition variable.</dd>
|
|
</dl>
|
|
|
|
<p>The <i>pthread_cond_init</i>() function may fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EBUSY]</dt>
|
|
|
|
<dd>The implementation has detected an attempt to reinitialize the object referenced by <i>cond</i>, a previously initialized, but
|
|
not yet destroyed, condition variable.</dd>
|
|
|
|
<dt>[EINVAL]</dt>
|
|
|
|
<dd>The value specified by <i>attr</i> is invalid.</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_516_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>A condition variable can be destroyed immediately after all the threads that are blocked on it are awakened. For example,
|
|
consider the following code:</p>
|
|
|
|
<pre>
|
|
<tt>struct list {
|
|
pthread_mutex_t lm;
|
|
...
|
|
}
|
|
<br>
|
|
struct elt {
|
|
key k;
|
|
int busy;
|
|
pthread_cond_t notbusy;
|
|
...
|
|
}
|
|
<br>
|
|
/* Find a list element and reserve it. */
|
|
struct elt *
|
|
list_find(struct list *lp, key k)
|
|
{
|
|
struct elt *ep;
|
|
<br>
|
|
pthread_mutex_lock(&lp->lm);
|
|
while ((ep = find_elt(l, k) != NULL) && ep->busy)
|
|
pthread_cond_wait(&ep->notbusy, &lp->lm);
|
|
if (ep != NULL)
|
|
ep->busy = 1;
|
|
pthread_mutex_unlock(&lp->lm);
|
|
return(ep);
|
|
}
|
|
<br>
|
|
delete_elt(struct list *lp, struct elt *ep)
|
|
{
|
|
pthread_mutex_lock(&lp->lm);
|
|
assert(ep->busy);
|
|
... remove ep from list ...
|
|
ep->busy = 0; /* Paranoid. */
|
|
(A) pthread_cond_broadcast(&ep->notbusy);
|
|
pthread_mutex_unlock(&lp->lm);
|
|
(B) pthread_cond_destroy(&rp->notbusy);
|
|
free(ep);
|
|
}
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>In this example, the condition variable and its list element may be freed (line B) immediately after all threads waiting for it
|
|
are awakened (line A), since the mutex and the code ensure that no other thread can touch the element to be deleted.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>See <a href="pthread_mutex_init.html"><i>pthread_mutex_init</i>()</a> ; a similar rationale applies to condition variables.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_516_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="pthread_cond_broadcast.html"><i>pthread_cond_broadcast</i>()</a> , <a href=
|
|
"pthread_cond_signal.html"><i>pthread_cond_signal</i>()</a> , <a href=
|
|
"pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</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_516_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_516_12"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>pthread_cond_destroy</i>() and <i>pthread_cond_init</i>() functions are marked as part of the Threads option.</p>
|
|
|
|
<p>IEEE PASC Interpretation 1003.1c #34 is applied, updating the DESCRIPTION.</p>
|
|
|
|
<p>The <b>restrict</b> keyword is added to the <i>pthread_cond_init</i>() prototype for alignment with the ISO/IEC 9899:1999
|
|
standard.</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>
|
|
|