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

238 lines
7.3 KiB
HTML
Raw Permalink 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_clear_exit_np()--Clear Exit Status of 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_clear_exit_np()--Clear Exit Status of Thread</h2>
<div class="box" style="width: 50%;">
<br>
&nbsp;&nbsp;Syntax:
<pre>
#include &lt;pthread.h
int pthread_clear_exit_np(void);
</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 <strong>pthread_clear_exit_np</strong>() function clears the exit status
of the thread. If the thread is currently exiting due to a call to
<strong>pthread_exit</strong>() or is the target of a
<strong>pthread_cancel</strong>(), then
<strong>pthread_clear_exit_np</strong>() can be used in conjunction with
<strong>setjmp</strong>(), <strong>longjmp</strong>(), and
<strong>pthread_setcancelstate</strong>() to prevent a thread from terminating,
and `handle' the exit condition.</p>
<p>The only supported way to prevent thread exit during the condition in which
<strong>pthread_exit</strong>() was called, or action is being taken for the
target of a <strong>pthread_cancel</strong>() is shown in the example. It
consists of using <strong>longjmp</strong>() from a cancellation cleanup
handler back into some thread routine that is still on the invocation stack.
From that routine, the functions <strong>pthread_clear_exit_np</strong>(), and
<strong>pthread_setcancelstate</strong>() are used to restore the state of the
thread before the condition that was causing the thread exit.</p>
<p><strong>Note:</strong> This function is not portable.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<p>None.</p>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_clear_exit_np</strong>() was successful.<br><br></dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_clear_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_clear_exit</strong>_np() 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 thread is not currently exiting</p></dd>
</dl>
<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_18.htm">pthread_exit()</a>--Terminate Calling Thread<br><br></li>
<li><a href="users_39.htm">pthread_cancel()</a>--Cancel 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 &lt;except.h&gt;
#include &lt;setjmp.h&gt;
#include "check.h"
int threadStatus=1;
void cleanupHandler(void *p)
{
jmp_buf *j = (jmp_buf *)p;
/* Warning, it is quite possible that using combinations of */
/* setjmp(), longjmp(), pthread_clear_exit_np(), and */
/* pthread_setcancelstate() to handle thread exits or */
/* cancellation could result in looping or non-cancelable */
/* threads if done incorrectly. */
printf("In cancellation cleanup handler. Handling the thread exit\n");
longjmp(*j, 1);
printf("The exit/cancellation was not stopped!\n");
return;
}
void *threadfunc(void *parm)
{
jmp_buf j;
int rc, old;
printf("Inside secondary thread\n");
if (setjmp(j)) {
/* Returned from longjmp after stopping the thread exit */
/* Since longjmp was called from within the cancellation */
/* cleanup handler, we must clear the exit state of the */
/* thread and reset the cancelability state to what it was */
/* before the cancellation cleanup handlers were called */
/* (Cancellation cleanup handlers are called with */
/* thread cancellation disabled) */
printf("Stopped the thread exit, now clean up the states\n");
printf("Clear exit state\n");
rc = pthread_clear_exit_np();
checkResults("pthread_clear_exit_np()\n", rc);
printf("Restore cancel state\n");
rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &amp;old);
checkResults("pthread_setcancelstate()\n", rc);
/* This example was successful */
threadStatus = 0;
}
else {
printf("Pushing cleanup handler that will stop the exit\n");
pthread_cleanup_push(cleanupHandler, &amp;j);
/* This exit will be stopped by cleanupHandler2 and the */
/* pthread_clear_exit_np() that is done above */
pthread_exit(__VOID(threadStatus));
printf("Did not expect to get here! Left status as 1.\n");
pthread_cleanup_pop(0);
}
pthread_exit(__VOID(threadStatus));
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
char c;
void *status;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create thread that will demonstrate handling an exit\n");
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
rc = pthread_join(thread, &amp;status);
checkResults("pthread_join()\n", rc);
if (__INT(status) != 0) {
printf("Got an unexpected return status from the thread!\n");
exit(1);
}
printf("Main completed\n");
return 0;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPCEXIT0
Create thread that will demonstrate handling an exit
Inside secondary thread
Pushing cleanup handler that will stop the exit
In cancellation cleanup handler. Handling the thread exit
Stopped the thread exit, now clean up the states
Clear exit state
Restore cancel state
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>