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

98 lines
3.3 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>Threads do not necessarily start before pthread_create() returns</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>Threads do not necessarily start before pthread_create() returns</h2>
<p>A thread may or may not start running before the return from <strong>
pthread_create</strong>(). Depending on the amount of time left in the creating
threads, time slice, and the other activity on the system, the creating thread
may return before the new thread runs.</p>
<p>The thread implementations of some systems guarantee a certain ordered
behavior for thread creation versus the execution of the first statement in the
new thread. On the iSeries, it is unknown which happens first, the execution of
the first instruction in the new thread or the return from <strong>
pthread_create</strong>().</p>
<p>The following example shows an incorrectly written application.</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"
pthread_t thread
void *threadfunc(void *parm)
{
pthread_id_np_t tid;
#error "This is an ERROR."
#error "The 'thread' variable is shared between threads"
#error "and must be protected by a mutex."
pthread_getunique_np(&amp;thread, &amp;tid);
printf("Thread 0x%.8x %.8x started\n", tid);
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
printf("Enter Testcase - %s\n", argv[0]);
#error "This is an ERROR."
#error "The order of thread thread startup, and return from"
#error "the pthread_create() API is NOT deterministic."
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)\n", rc);
/* sleep() isn't a very robust way to wait for the thread */
sleep(5);
printf("Main completed\n");
return 0;
}
</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>