The reference I'm using explains the two in the following way:
wait_for
"blocks the current thread until the condition variable is woken up or after the specified timeout duration"
wait_until
"blocks the current thread until the condition variable is woken up or until specified time point has been reached"
What is the difference? Will wait_until
spin so that the thread can continue exactly (more or less) when it is signaled, whereas wait_for
just adds the thread back into scheduling at that point?
std::condition_variable The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable .
The notify_all() member function unblocks all of the threads that are blocked on the specified condition variable at the time of the call. The order in which threads execute following a call to notify_all() is unspecified.
pub struct Condvar { /* private fields */ } A Condition Variable. Condition variables represent the ability to block a thread such that it consumes no CPU time while waiting for an event to occur. Condition variables are typically associated with a boolean predicate (a condition) and a mutex.
condition_variable::wait wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied (bool(stop_waiting()) == true).
The difference is in how the wait duration is represented: wait_for
takes a relative time ("wait for up to 10 seconds"), whereas wait_until
takes an absolute time ("wait until 12:00 on October 30, 2012").
Compare the declarations of the time parameters:
// wait_for: const std::chrono::duration<Rep, Period>& rel_time // wait_until: const std::chrono::time_point<Clock, Duration>& abs_time
Your question and other answers have all repeated the difference; that wait_for
waits for a specified amount of time and wait_until
waits until a specified point in time, but the implications are not spelled out.
A time_point
has an associated clock, and that clock is what's used to determine if the appropriate time has come. That means that clock adjustments are taken into account by the wait_until
function. wait_until(..., system_clock::now() + std::chrono::seconds(10))
could end up waiting an hour and 10 seconds if the clock happens to be adjusted back by an hour before the wait is up.
A duration does not have any associated clock and therefore wait_for
chooses its own clock. The standard specifies that it uses std::steady_clock, which cannot be adjusted and advances at a steady rate relative to real time. This means that wait_for
will wait for the specified time regardless of any adjustments made to any clocks. wait_for(..., std::chrono::seconds(10))
is guaranteed to wait 10 seconds (+ some time for the implementation to work and for scheduling issues).
There is no difference with regard to spinning vs. sleeping the thread; as wait_for
is specified to behave as if it called wait_until
with steady_clock::now() + duration
.
Here's the part of the standard where this is spelled out:
2 Implementations necessarily have some delay in returning from a timeout. Any overhead in interrupt response, function return, and scheduling induces a “quality of implementation” delay, expressed as duration Di. Ideally, this delay would be zero. Further, any contention for processor and memory resources induces a “quality of management” delay, expressed as duration Dm. The delay durations may vary from timeout to timeout, but in all cases shorter is better.
3 The member functions whose names end in
_for
take an argument that specifies a duration. These functions produce relative timeouts. Implementations should use a steady clock to measure time for these functions. Given a duration argument Dt, the real-time duration of the timeout is Dt + Di + Dm.4 The member functions whose names end in
_until
take an argument that specifies a time point. These functions produce absolute timeouts. Implementations should use the clock specified in the time point to measure time for these functions. Given a clock time point argument Ct, the clock time point of the return from timeout should be Ct + Di + Dm when the clock is not adjusted during the timeout. If the clock is adjusted to the time Ca during the timeout, the behavior should be as follows:
— if Ca > Ct, the waiting function should wake as soon as possible, i.e. Ca + Di + Dm, since the timeout is already satisfied. [ Note: This specification may result in the total duration of the wait decreasing when measured against a steady clock. —end note ]
— if Ca <= Ct, the waiting function should not time out untilClock::now()
returns a time Cn >= Ct, i.e. waking at Ct + Di + Dm. [ Note: When the clock is adjusted backwards, this specification may result in the total duration of the wait increasing when measured against a steady clock. When the clock is adjusted forwards, this specification may result in the total duration of the wait decreasing when measured against a steady clock. —end note ]
An implementation shall return from such a timeout at any point from the time specified above to the time it would return from a steady-clock relative timeout on the difference between Ct and the time point of the call to the_until
function. [ Note: Implementations should decrease the duration of the wait when the clock is adjusted forwards. —end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With