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

203 lines
7.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_cond_init()--Initialize Condition Variable</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_cond_init()--Initialize Condition Variable</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax:
<pre> #include &lt;pthread.h>
int pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; </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: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_cond_init</strong>() function initializes a condition variable object with the specified attributes for use. The new condition may be used immediately for serializing threads. If <em>attr</em> is specified as <strong>NULL</strong>, all attributes are set to the default condition attributes for the newly created condition.</p>
<p>With these declarations and initialization:</p>
<pre>
pthread_cond_t cond2;
pthread_cond_t cond3;
pthread_condattr_t attr;
pthread_condattr_init(&amp;attr);</pre>
<p>The following four condition variable initialization mechanisms have equivalent function:</p>
<pre>
pthread_cond_t cond1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_init(&amp;cond2, NULL);
pthread_cond_init(&amp;cond3, &amp;attr);</pre>
<p>All four condition variables are created with the default condition attributes.</p>
<p>Every condition variable must eventually be destroyed with <strong>pthread_cond_destroy</strong>().</p>
<p>Once a condition variable is created, it cannot be validly copied or moved to a new location. If the condition variable is copied or moved to a new location, the new object is not valid and cannot be used. Attempts to use the new object cause the <strong>EINVAL</strong> error.</p>
<p>Static initialization using the <strong>PTHREAD_COND_INITIALIZER</strong> does not immediately initialize the mutex. Instead, on first use, the functions <strong>pthread_cond_wait</strong>(), <strong>pthread_cond_timedwait</strong>(), <strong>pthread_cond_signal</strong>(), and <strong>pthread_cond_broadcast</strong>() branch into a slow path and cause the initialization of the condition. Due to this delayed initialization, the results of calling <strong>pthread_cond_destroy</strong>() on a condition variable that was initialized using static initialization and not used yet cause <strong>pthread_cond_destroy</strong>() to fail with teh <strong>EINVAL</strong> error.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>cond</strong></dt>
<dd>(Output) The address of the condition variable to initialize</dd>
<dt><strong>attr</strong></dt>
<dd>(Input) The address of the condition attributes object to use for initialization</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_cond_init</strong>() was successful.</dd>
<dt>
<strong>value</strong></dt>
<dd><strong>pthread_cond_init</strong>() was not successful. <em>value</em> is set to indicate the error condition.</dd>
</dl>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pthread_cond_init</strong>() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.</p>
<dl>
<dt><em>[EINVAL]</em></dt>
<dd><p>The value specified for the argument is not correct.</p></dd>
</dl>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>> header file. See <a href="rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li><a href="users_73.htm">pthread_cond_broadcast()</a>--Broadcast Condition to All Waiting Threads<br><br></li>
<li><a href="users_74.htm">pthread_cond_destroy()</a>--Destroy Condition Variable<br><br></li>
<li><a href="users_76.htm">pthread_cond_signal()</a>--Signal Condition to One Waiting Thread<br><br></li>
<li><a href="users_77.htm">pthread_cond_timedwait()</a>--Timed Wait for Condition<br><br></li>
<li><a href="users_78.htm">pthread_cond_wait()</a>--Wait for Condition</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>
#include &lt;stdio.h>
#include &quot;check.h&quot;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2;
pthread_cond_t cond3;
int main(int argc, char **argv)
{
int rc=0;
pthread_condattr_t attr;
printf(&quot;Enter Testcase - %s\n&quot;, argv[0]);
printf(&quot;Create the default cond attributes object\n&quot;);
rc = pthread_condattr_init(&amp;attr);
checkResults(&quot;pthread_condattr_init()\n&quot;, rc);
printf(&quot;Create the all of the default conditions in different ways\n&quot;);
rc = pthread_cond_init(&amp;cond2, NULL);
checkResults(&quot;pthread_cond_init()\n&quot;, rc);
rc = pthread_cond_init(&amp;cond3, &amp;attr);
checkResults(&quot;pthread_cond_init()\n&quot;, rc);
printf(&quot;- At this point, the conditions with default attributes\n&quot;);
printf(&quot;- Can be used from any threads that want to use them\n&quot;);
printf(&quot;Cleanup\n&quot;);
pthread_condattr_destroy(&amp;attr);
pthread_cond_destroy(&amp;cond1);
pthread_cond_destroy(&amp;cond2);
pthread_cond_destroy(&amp;cond3);
printf(&quot;Main completed\n&quot;);
return 0;
}</pre>
<p><strong>Output:</strong></p>
<pre>Enter Testcase - QP0WTEST/TPCOI0
Create the default cond attributes object
Create the all of the default conditions in different ways
- At this point, the conditions with default attributes
- Can be used from any threads that want to use them
Cleanup
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>