Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/pthread_join.html
2024-02-19 00:21:47 -05:00

216 lines
7.8 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_join</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="pthread_join"></a> <a name="tag_03_533"></a><!-- pthread_join -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_533_01"></a>NAME</h4>
<blockquote>pthread_join - wait for thread termination</blockquote>
<h4><a name="tag_03_533_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_join(pthread_t</tt> <i>thread</i><tt>, void **</tt><i>value_ptr</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_533_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>pthread_join</i>() function shall suspend execution of the calling thread until the target <i>thread</i> terminates,
unless the target <i>thread</i> has already terminated. On return from a successful <i>pthread_join</i>() call with a non-NULL
<i>value_ptr</i> argument, the value passed to <a href="../functions/pthread_exit.html"><i>pthread_exit</i>()</a> by the
terminating thread shall be made available in the location referenced by <i>value_ptr</i>. When a <i>pthread_join</i>() returns
successfully, the target thread has been terminated. The results of multiple simultaneous calls to <i>pthread_join</i>() specifying
the same target thread are undefined. If the thread calling <i>pthread_join</i>() is canceled, then the target thread shall not be
detached.</p>
<p>It is unspecified whether a thread that has exited but remains unjoined counts against {PTHREAD_THREADS_MAX}.</p>
</blockquote>
<h4><a name="tag_03_533_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If successful, the <i>pthread_join</i>() function shall return zero; otherwise, an error number shall be returned to indicate
the error.</p>
</blockquote>
<h4><a name="tag_03_533_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>pthread_join</i>() function shall fail if:</p>
<dl compact>
<dt>[EINVAL]</dt>
<dd>The implementation has detected that the value specified by <i>thread</i> does not refer to a joinable thread.</dd>
<dt>[ESRCH]</dt>
<dd>No thread could be found corresponding to that specified by the given thread ID.</dd>
</dl>
<p>The <i>pthread_join</i>() function may fail if:</p>
<dl compact>
<dt>[EDEADLK]</dt>
<dd>A deadlock was detected or the value of <i>thread</i> specifies the calling thread.</dd>
</dl>
<p>The <i>pthread_join</i>() function 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_533_06"></a>EXAMPLES</h4>
<blockquote>
<p>An example of thread creation and deletion follows:</p>
<pre>
<tt>typedef struct {
int *ar;
long n;
} subarray;
<br>
void *
incer(void *arg)
{
long i;
<br>
for (i = 0; i &lt; ((subarray *)arg)-&gt;n; i++)
((subarray *)arg)-&gt;ar[i]++;
}
<br>
int main(void)
{
int ar[1000000];
pthread_t th1, th2;
subarray sb1, sb2;
<br>
sb1.ar = &amp;ar[0];
sb1.n = 500000;
(void) pthread_create(&amp;th1, NULL, incer, &amp;sb1);
<br>
sb2.ar = &amp;ar[500000];
sb2.n = 500000;
(void) pthread_create(&amp;th2, NULL, incer, &amp;sb2);
<br>
(void) pthread_join(th1, NULL);
(void) pthread_join(th2, NULL);
return 0;
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_533_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_533_08"></a>RATIONALE</h4>
<blockquote>
<p>The <i>pthread_join</i>() function is a convenience that has proven useful in multi-threaded applications. It is true that a
programmer could simulate this function if it were not provided by passing extra state as part of the argument to the
<i>start_routine</i>(). The terminating thread would set a flag to indicate termination and broadcast a condition that is part of
that state; a joining thread would wait on that condition variable. While such a technique would allow a thread to wait on more
complex conditions (for example, waiting for multiple threads to terminate), waiting on individual thread termination is considered
widely useful. Also, including the <i>pthread_join</i>() function in no way precludes a programmer from coding such complex waits.
Thus, while not a primitive, including <i>pthread_join</i>() in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 was considered
valuable.</p>
<p>The <i>pthread_join</i>() function provides a simple mechanism allowing an application to wait for a thread to terminate. After
the thread terminates, the application may then choose to clean up resources that were used by the thread. For instance, after
<i>pthread_join</i>() returns, any application-provided stack storage could be reclaimed.</p>
<p>The <i>pthread_join</i>() or <a href="../functions/pthread_detach.html"><i>pthread_detach</i>()</a> function should eventually
be called for every thread that is created with the <i>detachstate</i> attribute set to PTHREAD_CREATE_JOINABLE so that storage
associated with the thread may be reclaimed.</p>
<p>The interaction between <i>pthread_join</i>() and cancelation is well-defined for the following reasons:</p>
<ul>
<li>
<p>The <i>pthread_join</i>() function, like all other non-async-cancel-safe functions, can only be called with deferred
cancelability type.</p>
</li>
<li>
<p>Cancelation cannot occur in the disabled cancelability state.</p>
</li>
</ul>
<p>Thus, only the default cancelability state need be considered. As specified, either the <i>pthread_join</i>() call is canceled,
or it succeeds, but not both. The difference is obvious to the application, since either a cancelation handler is run or
<i>pthread_join</i>() returns. There are no race conditions since <i>pthread_join</i>() was called in the deferred cancelability
state.</p>
</blockquote>
<h4><a name="tag_03_533_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_533_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="pthread_create.html"><i>pthread_create</i>()</a> , <a href="wait.html"><i>wait</i>()</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_533_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_533_12"></a>Issue 6</h4>
<blockquote>
<p>The <i>pthread_join</i>() function is marked as part of the Threads option.</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>