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

214 lines
6.5 KiB
HTML

<!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_test_exit_np()--Test Thread Exit Status</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_test_exit_np()--Test Thread Exit Status</h2>
<div class="box" style="width: 50%;">
<br>
&nbsp;&nbsp;Syntax:
<pre> #include &lt;pthread.h&gt;
#include &lt;sched.h&gt;
int pthread_test_exit_np(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: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The pthread_test_exit_np() function returns the current state of the thread along with its exit status.</p>
<p>If the thread is currently processing an exit condition due to a call to <strong>pthread_exit</strong>() or cancellation due to being the target of a <strong>pthread_cancel</strong>(), <strong>pthread_test_exit_np</strong>() returns <strong>PTHREAD_STATUS_EXIT_NP</strong> and sets the exit status pointed to by the status parameter to be the current thread exit status.</p>
<p>If the thread is currently running and is not running cancellation cleanup handlers or data destructors while terminating, <strong>pthread_test_exit_np()</strong> returns <strong>PTHREAD_STATUS_ACTIVE_NP</strong>, and does not return the exit status.</p>
<p><strong>Note:</strong> This function is not portable.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>status</strong></dt>
<dd>Pointer to the parameter to receive the exit status if <em>PTHREAD_STATUS_EXIT_NP</em> is returned</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>PTHREAD_STATUS_ACTIVE_NP</strong></dt>
<dd>The thread is currently not exiting.<br><br></dd>
<dt><strong>PTHREAD_STATUS_EXIT_NP</strong></dt>
<dd>The thread is currently exiting.<br><br></dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_test_exit_np()</strong> was not successful. <em>value</em> is set to indicate the error condition.</dd>
</dl>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pthread_test_exit_np()</strong> was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.</p>
<dl>
<dt><em>[EINVAL]</em></dt>
<dd><p>The values specified for the argument are not correct.</p></dd>
</dl>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>> 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_18.htm">pthread_exit()</a>--Terminate Calling 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 &quot;check.h&quot;
int checkStatusFailed1=0;
int missedHandler1=1;
int thread1Status=42;
void cleanupHandler1(void *arg)
{
int rc;
void *status;
printf(&quot;Thread 1 - cleanup handler\n&quot;);
missedHandler1=0;
rc = pthread_test_exit_np(&amp;status);
if (rc != PTHREAD_STATUS_EXIT_NP) {
printf(&quot;Thread 1 - returned %d instead &quot;
&quot;of PTHREAD_STATUS_EXIT_NP\n&quot;, rc);
checkStatusFailed1 = 1;
return;
}
if (__INT(status) != thread1Status) {
printf(&quot;Thread 1 - status = %d\n&quot;
&quot;Thread 1 - expected %d\n&quot;,
__INT(status), thread1Status);
checkStatusFailed1=1;
}
printf(&quot;Thread 1 - correctly got PTHREAD_STATUS_EXIT_NP &quot;
&quot;and thread exit status of %d\n&quot;, thread1Status);
}
void *thread1func(void *parm)
{
printf(&quot;Thread 1 - Entered\n&quot;);
pthread_cleanup_push(cleanupHandler1, NULL);
pthread_exit(__VOID(thread1Status));
pthread_cleanup_pop(0);
return __VOID(0);
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
void *status;
printf(&quot;Enter Testcase - %s\n&quot;, argv[0]);
rc = pthread_test_exit_np(&amp;status);
if (rc != PTHREAD_STATUS_ACTIVE_NP) {
printf(&quot;We should always be in an ACTIVE status here! rc=%d\n&quot;,
rc);
exit(1);
}
printf(&quot;Create the pthread_exit thread\n&quot;);
rc = pthread_create(&amp;thread, NULL, thread1func, NULL);
checkResults(&quot;pthread_create()\n&quot;, rc);
rc = pthread_join(thread, &amp;status);
checkResults(&quot;pthread_join()\n&quot;, rc);
if (__INT(status) != thread1Status) {
printf(&quot;Wrong status from thread 1\n&quot;);
}
if (checkStatusFailed1 ||&nbsp; missedHandler1) {
printf(&quot;The thread did not complete its test correctly! &quot;
&quot; check=%d, missed=%d\n&quot;,
checkStatusFailed1, missedHandler1);
exit(1);
}
printf(&quot;Main completed\n&quot;);
return 0;
}</pre>
<p><strong>Output:</strong></p>
<pre>Enter Testcase - QP0WTEST/TPTEXIT0
Create the pthread_exit thread
Thread 1 - Entered
Thread 1 - cleanup handler
Thread 1 - correctly got PTHREAD_STATUS_EXIT_NP and thread exit status of 42
Main completed</pre>
<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>