[英]sleep function in C11
我想睡在C11程序中。 usleep(在unistd.h中)和nanosleep(在time.h中)都沒有使用gcc(4.8.2)和clang(3.2)的-std=c11
選項聲明。
grep sleep /usr/include/*.h
沒有透露任何其他可能的睡眠候選者。
我需要一個至少毫秒精度的睡眠。
我怎么在C11睡覺?
使用-std=gnu11
而不是-std=c11
(這適用於clang和gcc)。 這將導致<time.h>
標頭定義nanosleep
。
nanosleep
另一種替代nanosleep
,在帶有超時的空文件描述符上調用pselect
,也只適用於-std=gnu11
而不是-std=c11
以下兩者為例:
#include <stdio.h>
#include <sys/select.h>
int main() // Compile with -std=gnu11 (and not -std=c11)
{
struct timespec ts1 = {
.tv_sec = 0,
.tv_nsec = 500*1000*1000
};
printf("first\n");
nanosleep(&ts1, NULL);
printf("second\n");
pselect(0, NULL, NULL, NULL, &ts1, NULL);
printf("third\n");
}
在<windows.h>
有一個Sleep函數,在<unistd.h>
一個usleep函數
他們兩個都得到了一個unsigned int
作為mili-seconds。
在threads.h頭文件中有thrd_sleep函數
int thrd_sleep( const struct timespec* time_point, struct timespec* remaining )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.