Timers and Sleeping 507
if (argc < 2)
usageErr("%s secs[/nsecs][:int-secs[/int-nsecs]]...\n", argv[0]);
tidlist = calloc(argc - 1, sizeof(timer_t));
if (tidlist == NULL)
errExit("malloc");
e sev.sigev_notify = SIGEV_THREAD; /* Notify via thread */
r sev.sigev_notify_function = threadFunc; /* Thread start function */
sev.sigev_notify_attributes = NULL;
/* Could be pointer to pthread_attr_t structure */
/* Create and start one timer for each command-line argument */
for (j = 0; j < argc - 1; j++) {
itimerspecFromStr(argv[j + 1], &ts);
t sev.sigev_value.sival_ptr = &tidlist[j];
/* Passed as argument to threadFunc() */
y if (timer_create(CLOCK_REALTIME, &sev, &tidlist[j]) == -1)
errExit("timer_create");
printf("Timer ID: %ld (%s)\n", (long) tidlist[j], argv[j + 1]);
u if (timer_settime(tidlist[j], 0, &ts, NULL) == -1)
errExit("timer_settime");
}
/* The main thread waits on a condition variable that is signaled
on each invocation of the thread notification function. We
print a message so that the user can see that this occurred. */
s = pthread_mutex_lock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
i for (;;) {
s = pthread_cond_wait(&cond, &mtx);
if (s != 0)
errExitEN(s, "pthread_cond_wait");
printf("main(): expireCnt = %d\n", expireCnt);
}
}
–––––––––––––––––––––––––––––––––––––––––––––––––timers/ptmr_sigev_thread.c
23.7 Timers That Notify via File Descriptors: the timerfd API
Starting with kernel 2.6.25, Linux provides another API for creating timers. The
Linux-specific timerfd API creates a timer whose expiration notifications can be
read from a file descriptor. This is useful because the file descriptor can be moni-
tored along with other descriptors using select(), poll(), and epoll (described in Chap-
ter 63). (With the other timer APIs discussed in this chapter, it requires some effort