liveBook · Manning

Absolute timeout

As explained in C++ Concurrency in Action, Second Edition

An absolute timeout is a deadline expressed as a particular point on a clock's timeline, rather than as a relative duration to wait.

An absolute timeout sets a deadline at a particular point on a clock’s timeline. Unlike a duration-based timeout, which specifies how long to wait, it says when to stop waiting. C++ waiting functions commonly distinguish the forms with `_until` and `_for` suffixes. section 4.3

A deadline is a `std::chrono::time_point` associated with a clock. Adding a duration to `some_clock::now()` creates a deadline; reusing that point across multiple waits keeps them within one time budget. The clock matters: adjustments can shorten or lengthen the wait as measured by a steady clock. section 4.3.3

Listing 4.11. Waiting for a condition variable with a timeout
#include <condition_variable>
#include <mutex>
#include <chrono>
std::condition_variable cv;
bool done;
std::mutex m;
bool wait_loop()
{
    auto const timeout= std::chrono::steady_clock::now()+
        std::chrono::milliseconds(500);
    std::unique_lock<std::mutex> lk(m);
    while(!done)
    {
        if(cv.wait_until(lk,timeout)==std::cv_status::timeout)
            break;
    }
    return done;
}

This condition-variable loop calculates one steady-clock deadline 500 milliseconds ahead and reuses it while checking whether the wait has completed. section 4.3.3

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Distinguishes deadlines from durations and explains time-point arithmetic and clock-adjustment effects.
Examples
Uses a condition-variable loop with a steady-clock deadline 500 milliseconds in the future.
What you can do after
Apply `wait_until` with a clock-based deadline to bound condition-variable waits and reuse a deadline across waits.
How it connects
Connects absolute timeouts to condition variables, clocks, time points, durations, and `wait_until`.

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.