[英]Why is clock_nanosleep preferred over nanosleep to create sleep times in C?
这两个功能中哪一个更好
#include <time.h>
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp);
要么
#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
clock_nanosleep
优于nanosleep
的优点是:
nanosleep
时间)时钟有所不同,可以由管理员或ntpd
等重置。通过nanosleep
并预先计算睡眠间隔达到给定的绝对时间,如果时钟是,则无法唤醒重置并且所需时间“早”到达。 此外,还有一个使用间隔时间进行调度的竞争条件:如果您计算要休眠的间隔,但是在调用nanosleep
之前先被抢占并且暂时不再安排,您将再次睡眠时间过长。 在我的系统上, man 2 clock_nanosleep
解释了这两个函数之间的差异:
Like nanosleep(2), clock_nanosleep() allows the caller to sleep for an interval specified with nanosecond precision. It differs in allowing the caller to select the clock against which the sleep interval is to be measured, and in allowing the sleep interval to be specified as either an absolute or a relative value. The clock_id argument [...] can have one of the following values: CLOCK_REALTIME A settable system-wide real-time clock. CLOCK_MONOTONIC A non-settable, monotonically increasing clock that measures time since some unspecified point in the past that does not change after system startup. CLOCK_PROCESS_CPUTIME_ID A settable per-process clock that measures CPU time consumed by all threads in the process.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.