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

136 lines
4.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>Return values from thread start routines are not integers</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>Return values from thread start routines are not integers</h2>
<p>Return values from a thread are defined to be of type <strong>void
*</strong>. On some platforms, a void * and an integer can be easily
interchanged with no loss of information. Until Version 4 Release 2, this was
not true on the iSeries. The iSeries enforces stricter pointer rules to both
prevent and detect application bugs or a malicious program's behavior. Thus,
when converting integers to pointers by a mechanism not directly supported by
your compiler, the valid pointer information is lost, and the pointer is always
set to <strong>NULL</strong> (regardless of its binary value).</p>
<p>New support put into the system in Version 4 Release 2 allows you to store
an integer into a pointer, and still have the pointer be
non-<strong>NULL</strong>. You cannot store to, read from, or defer a pointer
created by this mechanism, but the pointer appears
non-<strong>NULL</strong>.</p>
<p>The macros <strong>__INT</strong>() and <strong>__VOID</strong>() are
provided to aid in compatibility and allow you to easily store and retrieve
integer information in pointer variables even if your compiler does not support
the direct typecast. These macros allow explicit conversion from a pointer to
an integer and from an integer to a pointer.</p>
<p><strong>Note:</strong> The macros <strong>__INT</strong>() and <strong>
__VOID</strong>() result in function calls.</p>
<br>
<h3>Example</h3>
<p>The following example shows the correct way to store and retrieve integer
information in pointer variables.</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"
int main(int argc, char **argv)
{
void *status1 = __VOID(5);
void *status2 = __VOID(999);
if (status1 == NULL) {
printf("Status1 pointer is NULL\n");
}
else {
printf("Status1 pointer is non-NULL\n");
}
if (status1 == status2) {
printf("Both status variables as pointers are equal\n");
}
else {
if (status1 &lt; status2) {
printf("Status1 is greater than status2\n");
}
else {
if (status1 &lt; status2) {
printf("Status1 is less then status2\n");
}
else {
printf("The pointers are unordered!\n");
}
}
}
printf("Pointer values stored in status variables are:\n"
" status1 = %.8x %.8x %.8x %.8x\n"
" status2 = %.8x %.8x %.8x %.8x\n",
status1, status2);
printf("Integer values stored in status variables are:\n"
" status1 = %d\n"
" status2 = %d\n",
__INT(status1), __INT(status2));
return;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Status1 pointer is non-NULL
Status1 is less then status2
Pointer values stored in status variables are:
status1 = 80000000 00000000 00008302 00000005
status2 = 80000000 00000000 00008302 000003e7
Integer values stored in status variables are:
status1 = 5
status2 = 999
</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>