Namespaces
Variants
Actions

Talk:cpp/thread/condition variable/wait until

From cppreference.com
The clock tied to timeout_time is used, which means that adjustments of the clock are taken into account.

I was interested in how this is actually implemented for arbitrary clocks, so I've looked up the implementation of wait_until in libc++ and libstdc++. Both compute a duration from the absolute time point argument and the clock's now(). This duration is added to the current system clock's time. The resulting absolute time point is passed to pthread_cond_timedwait. So there's no magic here that watches for clock adjustments. Unless they're somehow propagated into pthread (like adjustments of system_clock are, as far as I know), adjustments of arbitrary (user-defined) clocks are not taken into account into the time waited. Of course, once pthread_cond_timedwait returns, the implementations look at how much time has actually passed, so the return value of wait_until can reflect adjustments of any custom clock. Unfortunately, this computation is not (cannot?) be performed atomically with the exit from pthread_cond_timedwait, so I think one can construct pathological cases where the return value doesn't accurately reflect clock adjustments as well. E.g. if the custom clock is set to an earlier time between the return of pthread_cond_timedwait and the computation of the return value for wait_until, the return value might be no_timeout, even though the absolute time point of the custom clock has expired.

Thus, the maximum duration of the block might, but might not, be less or more [...]

This sentence might, but might not, confuse me very much. IMO, the word "might" allows for the interpretation that it does not occur: "Thus, the maximum duration of the block might be less or more"

dyp --91.14.105.139 05:26, 2 July 2015 (PDT)

That paragraph was here since 2012, good find. POSIX spec is "For cases when the system clock is advanced discontinuously by an operator, it is expected that implementations process any timed wait expiring at an intervening time as if that time had actually occurred", but of course user-defined Clock means anything can happen. I think it would make sense to replace that paragraph with actual implementation details in a Notes section. --Cubbi (talk) 06:15, 2 July 2015 (PDT)
done --Cubbi (talk) 09:13, 14 July 2015 (PDT)


I think there are a mistake

Instead of: 2) Equivalent to while (!pred()) {

 if (wait_until(lock, abs_time) == std::cv_status::timeout) {
 return pred();
 }

} return true; // <------------------- this is wrong.

Should be: 2) Equivalent to while (!pred()) {

 if (wait_until(lock, abs_time) == std::cv_status::timeout) {
 return pred();
 }

} return false; // <------------------ should be false, not true.

— Preceding unsigned comment added by Maritimo (talkcontribs)

No, it is correct as written. The only way it can reach that return is if pred evaluated to true. T. Canens (talk) 02:29, 17 June 2018 (PDT)
Retrieved from "https://en.cppreference.com/mwiki/index.php?title=Talk:cpp/thread/condition_variable/wait_until&oldid=103921"

AltStyle によって変換されたページ (->オリジナル) /