为什么在 C 语言中创建睡眠时间时,clock_nanosleep 优于 nanosleep?
这两个功能哪一个更好
#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);
Which one of the two functions is better
#include <time.h>
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp);
OR
#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
clock_nanosleep
相对于nanosleep
的优点是:nanosleep
之前被抢占,并且一段时间内不会再次被调度,您将又睡太久了。The advantages of
clock_nanosleep
overnanosleep
are:ntpd
, etc. Withnanosleep
and precalculating the interval to sleep to reach a given absolute time, you'll fail to wake if the clock is reset and the desired time arrives "early". Also, there's a race condition with scheduling using interval times: If you compute the interval you want to sleep, but you get preempted before callingnanosleep
and don't get scheduled again for a while, you'll again sleep too long.在我的系统上,
man 2 Clock_nanosleep
解释了两个函数之间的差异:On my system,
man 2 clock_nanosleep
explains the differences between the two functions thus: