Library contents

C++ std::future_status::timeout

A future obtained from a promise returns timeout when its value is unavailable within the wait.

A simple example

std::future_status::timeout

  1. Start with An unfinished task
  2. Do this Wait only until a deadline
  3. Result Time limit reached
The wait can time out while the task itself is still running.

Syntax

C++ syntax
quote.wait_for(duration)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Limit the wait for a shipping quote

A checkout screen has used its entire waiting budget for a carrier quote. The quote is still pending, so the customer should see a pending message while the underlying work remains available.

ResultQuote pending
lesson.cpp
#include <iostream>
#include <future>
#include <chrono>

int main() {
    std::promise<int> carrier;
    auto quote = carrier.get_future();
    if (quote.wait_for(std::chrono::seconds(0)) == std::future_status::timeout) {
        std::cout << "Quote pending\n";
    }
    return 0;
}
Output
Quote pending

There is no result within the zero-length wait, so checkout reports a pending quote immediately.

Advertisements

Poll a finished download

A document download has already completed when the kiosk checks. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.

GivenCompleted future · timeout 0
Worked resultResult ready

Poll a pending proof

A designer checks a pending proof without blocking the screen. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.

GivenPending future · timeout 0
Worked resultStill waiting

What to watch for

Calling get immediately after a timeout can still block. Check the status first.

Documentation for std::future_status::timeout