?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<threads.h>中定義 | ||
---|---|---|
int thrd_sleep(const struct timespec * time_point,struct timespec * remaining); | (自C11以來(lái)) |
至少在達(dá)到time_point指向的基于TIME_UTC的時(shí)間點(diǎn)之前,阻止當(dāng)前線程的執(zhí)行。
如果收到未被忽略的信號(hào),睡眠可能會(huì)恢復(fù)。 在這種情況下,如果剩余時(shí)間不為NULL,則剩余持續(xù)時(shí)間將存儲(chǔ)到剩余指向的對(duì)象中。
time_point | - | 指向 sleep until 的時(shí)間點(diǎn) |
---|---|---|
remaining | - | 指向該對(duì)象的剩余時(shí)間中斷??赡転镹ULL,在這種情況下,它將被忽略 |
成功睡眠時(shí)返回0,發(fā)生信號(hào)時(shí)返回-1,發(fā)生錯(cuò)誤時(shí)返回負(fù)值。
time_point
與remaining
可以在相同的對(duì)象,該對(duì)象的信號(hào)之后簡(jiǎn)化重新運(yùn)行功能點(diǎn)。
實(shí)際的睡眠時(shí)間可能比請(qǐng)求的要長(zhǎng),因?yàn)樗鼤?huì)向上舍入到定時(shí)器粒度以及調(diào)度和上下文切換開(kāi)銷(xiāo)。
這個(gè)函數(shù)的POSIX相當(dāng)于nanosleep。
#include <threads.h>#include <time.h>#include <stdio.h> int main(void){ printf("Time: %s", ctime(&(time_t){time(NULL)})); thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec printf("Time: %s", ctime(&(time_t){time(NULL)}));}
輸出:
Time: Mon Feb 2 16:18:41 2015Time: Mon Feb 2 16:18:42 2015
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.26.5.7 thrd_sleep函數(shù)(p:385)
thrd_yield(C11) | 產(chǎn)生當(dāng)前時(shí)間片(功能) |
---|