我想睡在C11程序里。我们的睡眠(在unistd.h)和纳米睡眠(在time.h)都不是用gcc (4.8.2)和clang (3.2)的-std=c11选项声明的。
grep sleep /usr/include/*.h不显示任何其他可能的睡眠候选。
我需要至少毫秒精度的睡眠。
我怎么睡在C11?
发布于 2014-04-08 01:22:14
使用-std=gnu11而不是-std=c11 (这对clang和gcc都适用)。这将导致<time.h>头定义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");
}发布于 2014-04-07 22:48:56
在文件头中,有一个thrd_sleep函数
int thrd_sleep( const struct timespec* time_point, struct timespec* remaining )sleep
发布于 2017-02-20 08:25:10
在<windows.h>中有睡眠功能,在<unistd.h>中有睡眠功能
睡眠需要毫秒,睡眠需要微秒。
https://stackoverflow.com/questions/22924468
复制相似问题