Linux多线程学习 - sched_yield() CPU让权

本文详细介绍了sched_yield()函数的功能与用法,该函数用于使当前线程放弃CPU,以便其他同等或更高优先级的线程有机会运行。文章通过一个多线程程序示例展示了如何使用sched_yield()来提高线程间的合作性调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FROM: https://blog.csdn.net/xiaoyeyopulei/article/details/7965533

sched_yield()这个函数可以使 另一个级别等于或高于当前线程的线程先运行。如果没有符合条件的线程,那么这个函数将会立刻返回然后继续执行当前线程的程序。

在成功完成之后返回零,否则返回-1.


NAME

   sched_yield - yield the processor

SYNOPSIS

   #include <sched.h>
   int sched_yield(void);

DESCRIPTION

   sched_yield() causes the calling thread to relinquish the CPU.  
   The thread is moved to the end of the queue for its static 
   pri‐ority and a new thread gets to run.

RETURN VALUE

   On success, sched_yield() returns 0.  On error, -1 is returned, and
   errno is set appropriately.

ERRORS

   In the Linux implementation, sched_yield() always succeeds.

CONFORMING TO

   POSIX.1-2001.

看下面一个实例

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <errno.h>

#define            LOOPCONSTANT     1000
#define            THREADS          3

pthread_mutex_t    mutex = PTHREAD_MUTEX_INITIALIZER;
int                i, j, k, l;
static void checkResults ( char *string, int rc )
{
	if ( rc )
	{
		printf ( "Error on : %s, rc=%d",
		         string, rc );
		exit ( EXIT_FAILURE );
	}

	return;
}
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;
}

编译

gcc - o sched_yield - lphread sched_yield.c

运行结果

Enter Testcase - ./sched_yield
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值