?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
在頭文件<threads.h>中定義 | ||
---|---|---|
void thrd_yield(); | (自C11以來(lái)) |
向?qū)崿F(xiàn)提供一個(gè)提示,以重新調(diào)度線程的執(zhí)行,從而允許其他線程運(yùn)行。
(無(wú)).
(none).
此功能的確切行為取決于實(shí)現(xiàn),特別是關(guān)于正在使用的OS調(diào)度程序的機(jī)制以及系統(tǒng)的狀態(tài)。 例如,先入先出的實(shí)時(shí)調(diào)度程序(Linux中的SCHED_FIFO)會(huì)掛起當(dāng)前線程,并將其放在相同優(yōu)先級(jí)線程的隊(duì)列的后面,這些線程已準(zhǔn)備好運(yùn)行(如果沒(méi)有其他線程 線程的優(yōu)先級(jí)相同,收益率沒(méi)有影響)。
這個(gè)函數(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標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.26.5.8 thrd_yield函數(shù)(p:385)
thrd_sleep(C11) | 暫停執(zhí)行調(diào)用線程一段時(shí)間(函數(shù)) |
---|
| 用于yield的C ++文檔 |