简体   繁体   中英

Is sem_timedwait with EINTR-check guaranteed to wait >= the specified time?

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 (ie timeout is expected), is the while-loop guaranteed to exit at the time specified in ts (or slightly later)? Ie 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_.

Assuming the semaphore is not posted-to (ie timeout is expected), is the while-loop guaranteed to exit at the time specified in ts (or slightly later)? Ie is it guaranteed that the while-loop will not exit before the time specified in ts?

It depends on what you mean by "guaranteed", but the specifications for sem_timedwait do not provide for it to time out before the specified time has expired. It can fail sooner for other reasons, though, so in that sense no, it is not guaranteed that the example while loop will run for the full time specified.

In particular, even if all the arguments are valid and the call is not interrupted by a signal, sem_timedwait() is explicitly permitted to fail with EDEADLK to indicate that a deadlock was detected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM