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

207 lines
6.1 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_cleanup_push()--Push Cleanup Handler onto Cancellation Cleanup
Stack</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_cleanup_push()--Push Cleanup Handler onto Cancellation Cleanup
Stack</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax:
<pre>
#include &lt;pthread.h&gt;
void pthread_cleanup_push(void (*routine)(void *), void *arg);
</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_cleanup_push</strong>() function pushes a cancellation
cleanup routine onto the calling threads cancellation cleanup stack. When the
thread calls <strong>pthread_exit</strong>() or is cancelled by <strong>
pthread_cancel</strong>(), the cancellation cleanup handlers are called with
the argument <em>arg</em>.</p>
<p>The cancellation cleanup handlers are also called when they are removed
from the cancellation cleanup stack by a call to <strong>
pthread_cleanup_pop</strong>() and a non-zero <em>execute</em> argument is
specified.</p>
<p>The <strong>pthread_cleanup_push</strong>() and the matching <strong>
pthread_cleanup_pop</strong>() call should be in the same lexical scope (that is,
same level of brackets {}).</p>
<p>When the thread calls <strong>pthread_exit</strong>() or is cancelled by
<strong>pthread_cancel</strong>(), the cancellation cleanup handlers are
called with the argument specified by the <strong>
pthread_cleanup_push</strong>() call that the handler was registered with.</p>
<p>During this thread cancellation cleanup processing, the thread calls
cancellation cleanup handlers with cancellation disabled until the last
cancellation cleanup handler returns. The handlers are called in Last In,
First Out (LIFO) order. Automatic storage for the invocation stack frame of the
function that registered the handler are still present when the cancellation
cleanup handler is executed.</p>
<p>When a cancellation cleanup handler is called because of a call to <strong>
pthread_cleanup_pop</strong>(1), the cancellation cleanup handler does not
necessarily run with cancellation disabled. The cancellation state and
cancellation type are not changed by a call to <strong>
pthread_cleanup_pop</strong>(1).</p>
<p>A cancellation cleanup handler should not exit using <strong>
longjmp</strong>() or <strong>siglongjmp</strong>(). If a cleanup handler takes
an exception, the exception condition is handled and ignored and processing
continues. You can look in the job log of the job to see exception messages
generated by cancellation cleanup handlers.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>routine</strong></dt>
<dd>(Input) The cancellation cleanup routine</dd>
<dt><strong>arg</strong></dt>
<dd>(Input) Argument that is passed to the start routine if it is called</dd>
</dl>
<br>
<h3>Return Value</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_41.htm">pthread_cleanup_pop()</a>--Pop Cleanup Handler off of
Cancellation Cleanup Stack<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 "check.h"
void cleanupHandler(void *arg)
{
printf("In the cleanup handler\n");
}
void *threadfunc(void *parm)
{
printf("Entered secondary thread\n");
pthread_cleanup_push(cleanupHandler, NULL);
while (1) {
pthread_testcancel();
sleep(1);
}
pthread_cleanup_pop(0);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
printf("Enter Testcase - %s\n", argv[0]);
/* Create a thread using default attributes */
printf("Create thread using the NULL attributes\n");
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)\n", rc);
/* sleep() is not a very robust way to wait for the thread */
sleep(2);
printf("Cancel the thread\n");
rc = pthread_cancel(thread);
checkResults("pthread_cancel()\n", rc);
/* sleep() is not a very robust way to wait for the thread */
sleep(3);
printf("Main completed\n");
return 0;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPCLPU0
Create thread using the NULL attributes
Entered secondary thread
Cancel the thread
In the cleanup handler
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>