{"id":5315,"date":"2022-12-20T18:37:18","date_gmt":"2022-12-20T21:37:18","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/pthread_join-man3p\/"},"modified":"2022-12-20T18:37:18","modified_gmt":"2022-12-20T21:37:18","slug":"pthread_join-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/pthread_join-man3p\/","title":{"rendered":"PTHREAD_JOIN (man3p)"},"content":{"rendered":"<h1 align=\"center\">PTHREAD_JOIN<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">pthread_join \u2014 wait for thread termination<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <pthread.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int pthread_join(pthread_t <i>thread<\/i>, void **<i>value_ptr<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <i>pthread_exit<\/i>() 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>\n<p style=\"margin-left:11%; margin-top: 1em\">It is unspecified whether a thread that has exited but remains unjoined counts against {PTHREAD_THREADS_MAX}.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The behavior is undefined if the value specified by the <i>thread<\/i> argument to <i>pthread_join<\/i>() does not refer to a joinable thread.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The behavior is undefined if the value specified by the <i>thread<\/i> argument to <i>pthread_join<\/i>() refers to the calling thread.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If successful, the <i>pthread_join<\/i>() function shall return zero; otherwise, an error number shall be returned to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_join<\/i>() function may fail if: <b><br \/> EDEADLK<\/b><\/p>\n<p style=\"margin-left:22%;\">A deadlock was detected.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_join<\/i>() function shall not return an error code of <b>[EINTR]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">An example of thread creation and deletion follows:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">typedef struct { <br \/> int *ar; <br \/> long n; <br \/> } subarray;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void bodies manpages.csv script_extrae_body.sh script.sh usr <br \/> incer(void *arg) <br \/> { <br \/> long i;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">for (i = 0; i < ((subarray *)arg)->n; i++) <br \/> ((subarray *)arg)->ar[i]++; <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int main(void) <br \/> { <br \/> int ar[1000000]; <br \/> pthread_t th1, th2; <br \/> subarray sb1, sb2;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">sb1.ar = &#038;ar[0]; <br \/> sb1.n = 500000; <br \/> (void) pthread_create(&#038;th1, NULL, incer, &#038;sb1);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">sb2.ar = &#038;ar[500000]; <br \/> sb2.n = 500000; <br \/> (void) pthread_create(&#038;th2, NULL, incer, &#038;sb2);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">(void) pthread_join(th1, NULL); <br \/> (void) pthread_join(th2, NULL); <br \/> return 0; <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 POSIX.1-2017 was considered valuable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_join<\/i>() or <i>pthread_detach<\/i>() 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>\n<p style=\"margin-left:11%; margin-top: 1em\">The interaction between <i>pthread_join<\/i>() and cancellation is well-defined for the following reasons:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>The <i>pthread_join<\/i>() function, like all other non-async-cancel-safe functions, can only be called with deferred cancelability type.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>Cancellation cannot occur in the disabled cancelability state.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">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 cancelability state.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If an implementation detects that the value specified by the <i>thread<\/i> argument to <i>pthread_join<\/i>() does not refer to a joinable thread, it is recommended that the function should fail and report an <b>[EINVAL]<\/b> error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If an implementation detects that the value specified by the <i>thread<\/i> argument to <i>pthread_join<\/i>() refers to the calling thread, it is recommended that the function should fail and report an <b>[EDEADLK]<\/b> error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an <b>[ESRCH]<\/b> error.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>pthread_create<\/i>(), <i>wait<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <i>Section 4.12<\/i>, <i>Memory Synchronization<\/i>, <b><pthread.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  pthread_join \u2014 wait for thread termination <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1594,2204],"class_list":["post-5315","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-man3p","tag-pthread_join"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5315"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5315\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}