?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
在頭文件<threads.h>中定義 | ||
---|---|---|
void thrd_yield(); | (自C11以來) |
向?qū)崿F(xiàn)提供一個提示,以重新調(diào)度線程的執(zhí)行,從而允許其他線程運行。
(無).
(none).
此功能的確切行為取決于實現(xiàn),特別是關(guān)于正在使用的OS調(diào)度程序的機制以及系統(tǒng)的狀態(tài)。 例如,先入先出的實時調(diào)度程序(Linux中的SCHED_FIFO)會掛起當(dāng)前線程,并將其放在相同優(yōu)先級線程的隊列的后面,這些線程已準備好運行(如果沒有其他線程 線程的優(yōu)先級相同,收益率沒有影響)。
這個函數(shù)的POSIX相當(dāng)于sched_yield。
#include <stdio.h>#include <time.h>#include <threads.h> // utility function: difference between timespecs in microsecondsdouble usdiff(struct timespec s, struct timespec e){ double sdiff = difftime(e.tv_sec, s.tv_sec); long nsdiff = e.tv_nsec - s.tv_nsec; if(nsdiff < 0) return 1000000*(sdiff-1) + (1000000000L+nsdiff)/1000.0; else return 1000000*(sdiff) + nsdiff/1000.0;} // busy wait while yieldingvoid sleep_100us(){ struct timespec start, end; timespec_get(&start, TIME_UTC); do { thrd_yield(); timespec_get(&end, TIME_UTC); } while(usdiff(start, end) < 100.0);} int main(){ struct timespec start, end; timespec_get(&start, TIME_UTC); sleep_100us(); timespec_get(&end, TIME_UTC); printf("Waited for %.3f us\n", usdiff(start, end));}
可能的輸出:
Waited for 100.344 us
C11標準(ISO / IEC 9899:2011):
7.26.5.8 thrd_yield函數(shù)(p:385)
thrd_sleep(C11) | 暫停執(zhí)行調(diào)用線程一段時間(函數(shù)) |
---|
| 用于yield的C ++文檔 |