sched_yield()

Уступить исполнение другим потокам с одинаковым приоритетом

Прототип:

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

Библиотека:

libc

Описание:

The sched_yield() function checks to see if other threads, at the same priority as that of the calling thread, are READY to run. If so, the calling thread yields to them and places itself at the end of the READY thread queue. The sched_yield() function never yields to a lower-priority thread.

A higher-priority thread always forces a lower-priority thread to yield (that is, preempt) the instant the higher-priority thread becomes ready to run, without the need for the lower-priority thread to give up the processor by calling the sched_yield() or SchedYield() functions.

The sched_yield() function calls the kernel function SchedYield(), and may be more portable across realtime POSIX systems.


Note: You should avoid designing programs that contain busy wait loops. If you can't avoid them, you can use sched_yield() to reduce the system load at a given priority level. Note that a thread that calls sched_yield() in a tight loop will spend a great deal of time in the kernel, which will have a small effect on interrupt latency.

Возвращаемое значение:

0.

Примеры:

#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
int main( void )
{
int i;
for ( ; ; )
{
/* Process something... */
for ( i = 0 ; i < 1000 ; ++i )
fun();
/* Yield to anyone else at the same priority */
sched_yield();
}
return (EXIT_SUCCESS); /* Never reached */
}
int fun()
{
int i;
for ( i = 0 ; i < 10 ; ++i )
i += i;
return( i );
}

Классификация:

POSIX 1003.1 PS|THR

Точка остановки потока
Нет
Обработчик прерываний
Нет
Обработчик сигналов
Да
В потоке
Да

Тематические ссылки:

getprio(), sched_getparam(), sched_get_priority_max(), sched_get_priority_min(), sched_getscheduler(), sched_setparam(), sched_setscheduler(), SchedYield(), setprio(), sleep()




Предыдущий раздел: Описание API системной библиотеки