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
- Start with An unfinished task
- Do this Wait only until a deadline
- Result Time limit reached
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
./lessonLimit 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.
#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;
}
Quote pending
There is no result within the zero-length wait, so checkout reports a pending quote immediately.
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.
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.
What to watch for
Calling get immediately after a timeout can still block. Check the status first.