ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/users_18.htm

249 lines
8.4 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>pthread_exit()--Terminate Calling Thread</title>
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Begin Header Records ========================================== -->
<!-- NETMG2 SCRIPT A converted by B2H R4.1 (346) (CMS) by HOLTJM at -->
<!-- RCHVMW2 on 29 Jan 1999 at 10:01:37 -->
<!--File Edited November 2001 -->
<!--End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link -->
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
</script>
<a name="Top_Of_Page"></a>
<h2>pthread_exit()--Terminate Calling Thread</h2>
<div class="box" style="width: 50%;">
<br>
&nbsp;&nbsp;Syntax:
<pre>
#include &lt;pthread.h&gt;
void pthread_exit(void *status);
</pre>
&nbsp;&nbsp;Service Program Name: QP0WPTHR<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE <br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Signal Safe: No<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_exit</strong>() function terminates the calling thread,
making its exit <em>status</em> available to any waiting threads. Normally, a
thread terminates by returning from the start routine that was specified in the
<strong>pthread_create</strong>() call which started it. An implicit call to
<strong>pthread_exit</strong>() occurs when any thread returns from its start
routine. (With the exception of the initial thread, at which time an implicit
call to <strong>exit</strong>() occurs). The <strong>pthread_exit</strong>()
function provides an interface similar to <strong>exit</strong>() but on a
per-thread basis.</p>
<p>Note that in the i5/OS implementation of threads, the initial thread is
special. Termination of the initial thread by <strong>pthread_exit</strong>()
or any thread termination mechanism terminates the entire process.</p>
<p>The following activities occur in this order when a thread terminates by a
return from its start routine or <strong>pthread_exit</strong>() or thread
cancellation:</p>
<ol>
<li>Any cancellation cleanup handlers that have been pushed and not popped will
be executed in reverse order with cancellation disabled.</li>
<li>Data destructors are called for any thread specific data entries that have
a non NULL value for both the value and the destructor.</li>
<li>The thread terminates.</li>
<li>Thread termination may possibly cause the system to run i5/OS cancel
handlers (registered with the #pragma cancel_handler directive), or C++
destructors for automatic objects.</li>
<li>If thread termination is occurring in the initial thread, it will cause the
system to terminate all other threads, then run C++ static object destructors,
activation group cleanup routines and atexit() functions.</li>
<li>Any mutexes that are held by a thread that terminates, become `abandoned'
and are no longer valid. Subsequent calls by other threads that attempt to
acquire the abandoned mutex though <strong>pthread_mutex_lock</strong>() will
deadlock. Subsequent calls by other threads that attempt to acquire the
abandoned mutex through <strong>pthread_mutex_trylock</strong>() will return
<strong>EBUSY</strong>.</li>
<li>No release of any application visible process resources occur. This
includes but is not limited to mutexes, file descriptors, or any process level
cleanup actions.</li>
</ol>
<p>Do not call <strong>pthread_exit</strong>() from a cancellation cleanup
handler or destructor function that was called as a result of either an
implicit or explicit call to <strong>pthread_exit</strong>(). If
<strong>pthread_exit</strong>() is called from a cancellation cleanup handler,
the new invocation of <strong>pthread_exit</strong>() will continue
cancellation cleanup processing using the next cancellation cleanup handler
that was pushed. If <strong>pthread_exit</strong>() is called from a data
destructor, the new invocation of <strong>pthread_exit</strong>() will skip all
subsequent calls to any data destructors (regardless of the number of
destructor iterations that have completed), and terminate the thread.</p>
<p>Cleanup handlers and data destructors are not called when the application
calls exit() or abort() or otherwise terminates the process. Cleanup handlers
and data destructors are not called when a thread terminates by any proprietary
i5/OS mechanism other than the Pthread interfaces.</p>
<p>The meaning of the <em>status</em> parameter is determined by the
application except for the following conditions:</p>
<ol>
<li>When the thread has been canceled using <strong>pthread_cancel</strong>(),
the exit status of <strong>PTHREAD_CANCELED</strong> will be made
available.</li>
<li>When the thread has been terminated as a result of an unhandled i5/OS
exception, operator intervention or other proprietary i5/OS mechanism, the
exit status of <strong>PTHREAD_EXCEPTION_NP</strong> will be made
available.</li>
</ol>
<p>No address error checking is done on the <em>status</em> parameter. Do not
call <strong>pthread_exit</strong>() with, or return the address of, a variable
in a threads automatic storage. This storage will be unavailable after the
thread terminates.</p>
<p><strong>Note:</strong> If <strong>pthread_exit</strong>() is called by
application code after step 3 in the above list,
<strong>pthread_exit</strong>() will fail with the <strong>CPF1F81</strong>
exception. This indicates that the thread is already considered terminated by
the system, and <strong>pthread_exit</strong>() cannot continue. If your code
does not handle this exception, it will appear as if the call to
<strong>pthread_exit</strong>() was successful.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>status</strong></dt>
<dd>(Input) exit status of the thread</dd>
</dl>
<br>
<h3>Return Value</h3>
<p><strong>pthread_exit</strong>() does not return.</p>
<br>
<h3>Error Conditions</h3>
<p>None.</p>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>&gt; header file. See <a href=
"rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li><a href="users_39.htm">pthread_cancel()</a>--Cancel Thread<br><br></li>
<li><a href="users_14.htm">pthread_create()</a>--Create Thread<br><br></li>
<li><a href="users_25.htm#">pthread_join()</a>--Wait for and Detach Thread</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<pre>
#define _MULTI_THREADED
#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include "check.h"
int theStatus=5;
void *threadfunc(void *parm)
{
printf("Inside secondary thread\n");
pthread_exit(__VOID(theStatus));
return __VOID(theStatus); /* Not needed, but this makes the compiler smile */
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
void *status;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create thread using attributes that allow join\n");
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
printf("Wait for the thread to exit\n");
rc = pthread_join(thread, &amp;status);
checkResults("pthread_join()\n", rc);
if (__INT(status) != theStatus) {
printf("Secondary thread failed\n");
exit(1);
}
printf("Got secondary thread status as expected\n");
printf("Main completed\n");
return 0;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPEXIT0
Create thread using attributes that allow join
Wait for the thread to exit
Inside secondary thread
Got secondary thread status as expected
Main completed
</pre>
<br>
<hr>
API introduced: V4R3
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"rzah4mst.htm">Pthread APIs</a> | <a href="aplist.htm">APIs by
category</a></td>
</tr>
</table>
</center>
</body>
</html>