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

252 lines
7.4 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>C++ destructors and Pthread termination</title>
<!-- Begin Header Records ========================================== -->
<!-- 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. -->
<!-- Change History: -->
<!-- YYMMDD USERID Change description -->
<!-- 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>C++ destructors and Pthread termination</h2>
<p>Unlike some other implementations of threads, C++ destructors for automatic
objects are allowed to run in a well defined and consistent manner when a
thread is terminated.</p>
<p>The following list includes some of the causes of thread termination:</p>
<ul>
<li>A thread calls <strong>pthread_exit</strong>() or returns from the thread
start routine.</li>
<li>A thread is the target of <strong>pthread_cancel</strong>().</li>
<li>A thread is ended due to an unhandled exception.</li>
<li>A thread is in a process that contains another thread that calls <strong>
exit</strong>() or <strong>abort</strong>().</li>
<li>A thread is in a process that is terminated by the system
administrator.</li>
<li>A thread is being terminated by the system administrator.</li>
</ul>
<p>When a thread terminates, the following occurs:</p>
<ol>
<li>If the thread was ended using <strong>pthread_exit</strong>(), <strong>
pthread_cancel</strong>() or return from the thread start routine, then
cancellation cleanup handlers and data destructors are run.</li>
<li>The thread is terminated. At the time that the thread is terminated, both
C++ destructors for automatic objects and i5/OS cancel handlers run.</li>
</ol>
<p>If a Pthread is terminated using a non-Pthread method (an i5/OS exception,
a different thread termination primitive provided by the system, <strong>
exit</strong>() or <strong>abort</strong>(), or other job termination method),
Pthread cancellation cleanup handlers and data destructors do not run.</p>
<br>
<h3>Example</h3>
<p>This example shows the relationship between C++ destructors and Pthread
cleanup mechanisms.</p>
<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;stdio.h&gt;
#include &lt;qp0z1170.h&gt;
#include &lt;time.h&gt;
#include &lt;pthread.h&gt;
#include "check.h"
#define bufferSize 100
#define threadRc 55
pthread_key_t tlskey;
void dataDestructor(void *parm);
void cancelHandler(void *parm);
void *threadfunc(void *parm);
void level2(void);
void level3(void);
class A {
public:
A(char *label);
~A();
private:
pthread_id_np_t tid;
char buffer[bufferSize];
};
void dataDestructor(void *parm) {
printf("In data destructor\n");
pthread_setspecific(tlskey, NULL);
}
void cancelHandler(void *parm) {
printf("In cancellation cleanup handler\n");
}
void *threadfunc(void *parm) {
A object("start routine object");
level2();
return NULL;
}
void level2(void) {
A object("Second level object");
level3();
}
void level3(void) {
int rc;
struct timespec ts = {5, 0};
A object("Third level object");
pthread_setspecific(tlskey, &amp;tlskey);
pthread_cleanup_push(cancelHandler, NULL);
printf("Thread blocked\n");
rc = pthread_delay_np(&amp;ts);
if (rc != 0) {
printf("pthread_delay_np() - return code %d\n", rc);
return;
}
printf("Calling pthread_exit()\n");
pthread_exit(__VOID(threadRc));
pthread_cleanup_pop(0);
}
int main(int argc, char **argv)
{
int rc=0;
int i;
pthread_t threadid;
void *status;
int fail=0;
printf("Enter Testcase - %s\n", argv[0]);
rc = pthread_key_create(&amp;tlskey, dataDestructor);
checkResults("pthread_key_create()\n", rc);
printf("----------- Start pthread_cancel() example -------------\n");
printf("Create a thread\n");
rc = pthread_create(&amp;threadid, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
sleep(2);
rc = pthread_cancel(threadid);
checkResults("pthread_cancel()\n", rc);
rc = pthread_join(threadid, &amp;status);
checkResults("pthread_join()\n", rc);
if (status != PTHREAD_CANCELED) {
printf("Canceled thread did not return the expected results\n");
fail = 1;
}
printf("----------- Start pthread_exit() example -------------\n");
printf("Create a thread\n");
rc = pthread_create(&amp;threadid, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
rc = pthread_join(threadid, &amp;status);
checkResults("pthread_join()\n", rc);
if (__INT(status) != threadRc) {
printf("pthread_exit() thread did not return the expected results\n");
fail = 1;
}
pthread_key_delete(tlskey);
if (fail) {
printf("At least one thread failed!\n");
exit(1);
}
printf("Main completed\n");
return 0;
}
A::A(char *label) {
strncpy(buffer, label, bufferSize);
pthread_t me;
me = pthread_self();
pthread_getunique_np(&amp;me, &amp;tid);
printf("`%s' instantiated in thread 0x%.8x %.8x\n",
buffer, tid);
}
A::~A() {
printf("`%s' destroyed in thread 0x%.8x %.8x\n",
buffer, tid);
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPCPP0
----------- Start pthread_cancel() example -------------
Create a thread
`start routine object' instantiated in thread 0x00000000 00000161
`Second level object' instantiated in thread 0x00000000 00000161
`Third level object' instantiated in thread 0x00000000 00000161
Thread blocked
In cancellation cleanup handler
In data destructor
`Third level object' destroyed in thread 0x00000000 00000161
`Second level object' destroyed in thread 0x00000000 00000161
`start routine object' destroyed in thread 0x00000000 00000161
----------- Start pthread_exit() example -------------
Create a thread
`start routine object' instantiated in thread 0x00000000 00000162
`Second level object' instantiated in thread 0x00000000 00000162
`Third level object' instantiated in thread 0x00000000 00000162
Thread blocked
Calling pthread_exit()
In cancellation cleanup handler
In data destructor
`Third level object' destroyed in thread 0x00000000 00000162
`Second level object' destroyed in thread 0x00000000 00000162
`start routine object' destroyed in thread 0x00000000 00000162
Main completed
</pre>
<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>