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

117 lines
3.8 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>MCH3402 from pointer returned by pthread_join()</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>MCH3402 from pointer returned by pthread_join()</h2>
<p>Be sure that no threads return pointers to items that can be destroyed when
a thread terminates. For example, the threads stack is transitory. It needs to
exist only for the life of the thread, and it may be destroyed when the thread
terminates. If you return the address of an automatic variable or use the
address of an automatic variable as an argument to pthread_exit(), you may
experience MCH3402 errors when you use the address.</p>
<br>
<h3>Example</h3>
<p>The following example contains code that brings up the MCH3402 error.</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;pthread.h&gt;
#include &lt;stdio.h&gt;
#include "check.h"
void *threadfunc(void *parm)
{
int rc = 2;
printf("Inside secondary thread, return address of local variable.\n");
return &amp;rc; /* THIS IS AN ERROR! */
/* AT THIS POINT, THE STACK FOR THIS THREAD MAY BE DESTROYED */
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=1;
void *status;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create thread that returns status incorrectly\n");
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
printf("Join to thread\n");
rc = pthread_join(thread, &amp;status);
checkResults("pthread_join()\n", rc);
printf("Checking results from thread. Expect MCH3402\n");
/* Monitor for the MCH3402 exception in this range */
#pragma exception_handler(TestOk, 0, 0, _C2_ALL, _CTLA_HANDLE_NO_MSG, "MCH3402")
rc = *(int *)status;
#pragma disable_handler
TestFailed:
printf("Did not get secondary thread results (exception) as expected!\n");
goto TestComplete;
TestOk: /* Control goes here for an MCH3402 exception */
printf("Got an MCH3402 as expected\n");
TestComplete:
printf("Main completed\n");
return rc;
}
</pre>
<p><strong>Output</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPJOIN7
Create thread that returns status incorrectly
Join to thread
Inside secondary thread, return address of local variable.
Checking results from thread. Expect MCH3402
Got an MCH3402 as expected
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>