C11的睡眠功能 [英] sleep function in C11

查看:18
本文介绍了C11的睡眠功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C11节目中睡觉。不会同时使用GCC(4.8.2)和clang(3.2)的-std=c11选项声明usept(unistd.h)或nanosept(time.h)。

Agrep sleep /usr/include/*.h未显示任何其他可能的睡眠候选项。

我需要至少毫秒精度的睡眠。

我如何在C11中入睡?

推荐答案

使用-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
");
   nanosleep(&ts1, NULL);
   printf("second
");
   pselect(0, NULL, NULL, NULL, &ts1, NULL);
   printf("third
");
}

这篇关于C11的睡眠功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆