0

I saw in usleep man that :

EINVAL
usec is not smaller than 1000000. (On systems where that is considered an error.) 

So i wonder if its ok to use usleep in Ubuntu with value greater than 1000000 and if not (or in case that i want to support other platforms ) what is the alternative when i need sleep for 2.2 sec (for example) .

Thank you.

4

2 回答 2

1

您必须查看 Linux 内核源代码才能 100% 确定,但考虑到 Ubuntu 仅针对 x86 和 x86-64 分发,并且无论底层POSIX是否允许,很快就会有人发现这种行为是不可接受的spec,它永远不会崩溃的可能性基本上为零。

Linux 被移植到各种各样的系统上带有草率的内核。

于 2012-08-29T09:59:58.707 回答
1

一种替代方法是信任文档并使用循环来实现它,以确保安全:

#define USLEEP_MAX (1000000 - 1)

void long_sleep(unsigned long micros)
{
  while(micros > 0)
  {
    const unsigned long chunk = micros > USLEEP_MAX ? USLEEP_MAX : micros;
    usleep(chunk);
    micros -= chunk;
  }
}

您还应该检查 的返回值usleep(),为简洁起见,我省略了它。

在生产中,您可以与Autoconf和朋友一起在编译时检测正确性USLEEP_MAX,如果本地系统没有参数限制,甚至可以切换到普通包装器。可以享受数小时的乐趣。

于 2012-08-29T09:50:27.160 回答