208 lines
5.3 KiB
HTML
208 lines
5.3 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>sched_yield()--Yield Processor to Another Thread</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>sched_yield()--Yield Processor to Another Thread</h2>
|
|
|
|
<div class="box" style="width: 50%;">
|
|
<br>
|
|
Syntax:
|
|
|
|
<pre>
|
|
#include <sched.h>
|
|
int sched_yield(void);
|
|
</pre>
|
|
Service Program Name: QP0WPTHR<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Default Public Authority: *USE<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Threadsafe: Yes<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Signal Safe: Yes<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
</div>
|
|
|
|
<p>The <strong>sched_yield</strong>() function yields the processor from the
|
|
currently executing thread to another ready-to-run, active thread of equal or
|
|
higher priority.</p>
|
|
|
|
<p>If no threads of equal or higher priority are active and ready to run,
|
|
<strong>sched_yield</strong>() returns immediately, and the calling thread
|
|
continues to run until its time has expired.</p>
|
|
|
|
<br>
|
|
<h3>Authorities and Locks</h3>
|
|
|
|
<p>None.</p>
|
|
|
|
<br>
|
|
<h3>Parameters</h3>
|
|
|
|
<p>None.</p>
|
|
|
|
<br>
|
|
<h3>Return Value</h3>
|
|
|
|
<dl>
|
|
<dt><strong>0</strong></dt>
|
|
|
|
<dd><strong>sched_yield</strong>() was successful.<br><br></dd>
|
|
|
|
<dt><strong>value</strong></dt>
|
|
|
|
<dd><strong>sched_yield</strong>() was not successful. <em>value</em> is set to
|
|
indicate the error condition.</dd>
|
|
</dl>
|
|
|
|
<br>
|
|
<h3>Error Conditions</h3>
|
|
|
|
<p>The <strong>sched_yield</strong>() API does not currently return an
|
|
error.</p>
|
|
|
|
<br>
|
|
<h3>Related Information</h3>
|
|
|
|
<ul>
|
|
<li>The <<strong>pthread.h</strong>> header file. See <a href=
|
|
"rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
|
|
|
|
<li><a href="users_21.htm">pthread_getschedparam()</a>--Get Thread Scheduling
|
|
Parameters</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 <pthread.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include "check.h"
|
|
|
|
#define LOOPCONSTANT 1000
|
|
#define THREADS 3
|
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
int i,j,k,l;
|
|
|
|
void *threadfunc(void *parm)
|
|
{
|
|
int loop = 0;
|
|
int localProcessingCompleted = 0;
|
|
int numberOfLocalProcessingBursts = 0;
|
|
int processingCompletedThisBurst = 0;
|
|
int rc;
|
|
|
|
printf("Entered secondary thread\n");
|
|
for (loop=0; loop<LOOPCONSTANT; ++loop) {
|
|
rc = pthread_mutex_lock(&mutex);
|
|
checkResults("pthread_mutex_lock()\n", rc);
|
|
/* Perform some not so important processing */
|
|
i++, j++, k++, l++;
|
|
|
|
rc = pthread_mutex_unlock(&mutex);
|
|
checkResults("pthread_mutex_unlock()\n", rc);
|
|
/* This work is not too important. Also, we just released a lock
|
|
and would like to ensure that other threads get a chance in
|
|
a more co-operative manner. This is an admittedly contrived
|
|
example with no real purpose for doing the sched_yield().
|
|
*/
|
|
sched_yield();
|
|
}
|
|
printf("Finished secondary thread\n");
|
|
return NULL;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
pthread_t threadid[THREADS];
|
|
int rc=0;
|
|
int loop=0;
|
|
|
|
printf("Enter Testcase - %s\n", argv[0]);
|
|
|
|
rc = pthread_mutex_lock(&mutex);
|
|
checkResults("pthread_mutex_lock()\n", rc);
|
|
|
|
printf("Creating %d threads\n", THREADS);
|
|
for (loop=0; loop<THREADS; ++loop) {
|
|
rc = pthread_create(&threadid[loop], NULL, threadfunc, NULL);
|
|
checkResults("pthread_create()\n", rc);
|
|
}
|
|
|
|
sleep(1);
|
|
rc = pthread_mutex_unlock(&mutex);
|
|
checkResults("pthread_mutex_unlock()\n", rc);
|
|
|
|
printf("Wait for results\n");
|
|
for (loop=0; loop<THREADS; ++loop) {
|
|
rc = pthread_join(threadid[loop], NULL);
|
|
checkResults("pthread_join()\n", rc);
|
|
}
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
printf("Main completed\n");
|
|
return 0;
|
|
}
|
|
</pre>
|
|
|
|
<p><strong>Output:</strong></p>
|
|
|
|
<pre>
|
|
Enter Testcase - QP0WTEST/TPSCHY0
|
|
Creating 3 threads
|
|
Entered secondary thread
|
|
Entered secondary thread
|
|
|
|
Entered secondary thread
|
|
Wait for results
|
|
Finished secondary thread
|
|
Finished secondary thread
|
|
Finished secondary thread
|
|
Main completed
|
|
</pre>
|
|
<br>
|
|
<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>
|
|
|