📄 pthread_join.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-2004 IEEE and 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, 2004 Edition<br>Copyright © 2001-2004 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 <<a href="../basedefs/pthread.h.html">pthread.h</a>><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 theterminating thread shall be made available in the location referenced by <i>value_ptr</i>. When a <i>pthread_join</i>() returnssuccessfully, the target thread has been terminated. The results of multiple simultaneous calls to <i>pthread_join</i>() specifyingthe same target thread are undefined. If the thread calling <i>pthread_join</i>() is canceled, then the target thread shall not bedetached.</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 indicatethe 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>[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><dt>[EINVAL]</dt><dd>The value specified by <i>thread</i> does not refer to a joinable 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 < ((subarray *)arg)->n; i++) ((subarray *)arg)->ar[i]++;}<br>int main(void){ int ar[1000000]; pthread_t th1, th2; subarray sb1, sb2;<br> sb1.ar = &ar[0]; sb1.n = 500000; (void) pthread_create(&th1, NULL, incer, &sb1);<br> sb2.ar = &ar[500000]; sb2.n = 500000; (void) pthread_create(&th2, NULL, incer, &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 aprogrammer 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 ofthat state; a joining thread would wait on that condition variable. While such a technique would allow a thread to wait on morecomplex conditions (for example, waiting for multiple threads to terminate), waiting on individual thread termination is consideredwidely 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 Std 1003.1-2001 was consideredvaluable.</p><p>The <i>pthread_join</i>() function provides a simple mechanism allowing an application to wait for a thread to terminate. Afterthe 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 eventuallybe called for every thread that is created with the <i>detachstate</i> attribute set to PTHREAD_CREATE_JOINABLE so that storageassociated with the thread may be reclaimed.</p><p>The interaction between <i>pthread_join</i>() and cancellation 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 deferredcancelability type.</p></li><li><p>Cancellation 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 cancellation handler is run or<i>pthread_join</i>() returns. There are no race conditions since <i>pthread_join</i>() was called in the deferred cancelabilitystate.</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 volumeof IEEE Std 1003.1-2001, <a href="../basedefs/pthread.h.html"><i><pthread.h></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><p>IEEE Std 1003.1-2001/Cor 2-2004, XSH/TC2/D6/97 is applied, updating the ERRORS section so that the [EINVAL] erroris made optional and the words ``the implementation has detected'' are removed from it.</p></blockquote><div class="box"><em>End of informative text.</em></div><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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -