![]() | |||||||||||||
Messages in this thread |
Jamie Lokier wrote:
[snip]
>
> Any code which is structured like this will break:
>
> time_t timeout = time(0) + TIMEOUT_IN_SECONDS;
>
> do {
> /* Do some stuff which takes a little while. */
> } while (time(0) <= timeout);
>
> It goes wrong when time() returns a value that is in the past, and
> then jumps forward to the correct time suddenly. The timeout of the
> above code is reduced by the size of that jump. If the jump is larger
> than TIMEOUT_IN_SECONDS, the timeout mechanism is defeated completely.
>
> That sort of code is a prime candidate for the method of using a
> worker thread updating a global variable, so it's really important to
> to take care when using it.
>
But isn't this kind of code a known buggy way of implementing timeouts?
Shouldn't it be like:
time_t x = time(0);
do {
...
} while (time(0) - x >= TIMEOUT_IN_SECONDS);
Ofcourse it can't handle times in the past, but it won't get easily hung
with regards to leaps or wraparounds (if used with other functions).
Regards
Magnus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
| ||||||||||||
| Last update: 2005-03-22 13:01 [from the cache] ©2003-2008 | |||||||||||||