阅读761 返回首页    go 阿里云 go 技术社区[云栖]


clock_nanosleep避免过度睡眠

/*
 *   “oversleeping” problem is particularly marked for a process that uses a loop to re start
 *   a sleep that is interrupted by a signal handler. If signals are delivered at a high rate
 *   , then a relative sleep (of the type performed by nanosleep()) can lead to large inaccuracies 
 *   in the time a process spends sleeping. We can avoid the oversleeping problem by making an
 *   initial call to clock_gettime() to retrieve the time, adding the  desired amount to that time, 
 *   and then calling  clock_nanosleep() with the TIMER_ABSTIME  flag (and restarting the system
 *   call if it is interrupted by a signal handler).
 */

struct timespec request;
/* Retrieve current value of CLOCK_REALTIME clock */
if (clock_gettime(CLOCK_REALTIME, &request) == -1) 
    errExit("clock_gettime");
request.tv_sec += 20;               /* Sleep for 20 seconds from now */
s = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &request, NULL); 
if (s != 0) {
    if (s == EINTR)
        printf("Interrupted by signal handler\n");
    else
        errExitEN(s, "clock_nanosleep");
}

最后更新:2017-04-03 12:55:25

  上一篇:go android自定义控件并添加属性的方法以及示例
  下一篇:go Web API 版本控制的几种方式