An oft-recommended approach to timed-waiting on a semaphore is (simplified for brevity):
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 5;
ts.tv_nsec += 3;
while (sem_timedwait(&sem, &ts) == -1 && errno == EINTR)
continue;
Assuming the semaphore is not posted-to (i.e. timeout is expected), is the while-loop guaranteed to exit at the time specified in ts
(or slightly later)? I.e. is it guaranteed that the while-loop will not exit before the time specified in ts
?
I half-remember observing sem_timedwait()
exiting slightly before the time specified in ts
- but I can't remember if that was because I didn't use the EINTR
-check. I do remember that there was a time I didn't quite understand what the EINTR
-check was for, so I used sem_timedwait()
alone, not in combination with the while-loop and EINTR
-check_.