std::future::wait_for(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay so bestie, welcome to std::future::wait_for(3) from the C++ Standard Libary manual ✨. The page literally says: std::future::wait_for β€” std::future::wait_for 😭 (iconic naming, no notes).

✨ Synopsis ✨

(since C++11) πŸ‘‘ here's the signature, copied exactly:

template< class Rep, class Period >
std::future_status wait_for( const std::chrono::duration<Rep,Period>&
timeout_duration ) const;

This function waits for the result to become available. It blocks until the specified timeout_duration has elapsed or the result becomes available β€” whichever comes first. The return value identifies the state of the result.

If the future is the result of a call to std::async that used lazy evaluation, this function returns immediately without waiting.

This function may block for longer than timeout_duration due to scheduling or resource contention delays. lowkey important 🫢

The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.

real talk bestie: the behavior is undefined if valid() is false before the call to this function πŸ’€ please don't do that.

πŸŽ€ Parameters πŸŽ€

  • timeout_duration β€” maximum duration to block for.

πŸ¦‹ Return value πŸ¦‹

  • future_status::deferred β€” the shared state contains a deferred function using lazy evaluation, so the result will be computed only when explicitly requested.
  • future_status::ready β€” the result is ready.
  • future_status::timeout β€” the timeout has expired.

🌸 Exceptions 🌸

Any exception thrown by clock, time_point, or duration during the execution (clocks, time points, and durations provided by the standard library never throw).

πŸ’… Notes πŸ’…

The implementations are encouraged to detect the case when valid == false before the call and throw a std::future_error with an error condition of std::future_errc::no_state.

πŸ“ Example πŸ“

Run this code (the page says // Run this code):

#include <chrono>
#include <future>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;

int main()
{
    std::future<int> future = std::async(std::launch::async, []()
    {
        std::this_thread::sleep_for(3s);
        return 8;
    });

    std::cout << "waiting...\n";
    std::future_status status;

    do
    {
        switch (status = future.wait_for(1s); status)
        {
            case std::future_status::deferred:
                std::cout << "deferred\n";
                break;
            case std::future_status::timeout:
                std::cout << "timeout\n";
                break;
            case std::future_status::ready:
                std::cout << "ready!\n";
                break;
        }
    }
    while (status != std::future_status::ready);

    std::cout << "result is " << future.get() << '\n';
}

Possible output:

waiting...
timeout
timeout
timeout
ready!
result is 8

🌈 See also 🌈

  • wait β€” waits for the result to become available (public member function).
  • wait_until β€” waits for the result, returns if it is not available until specified time point has been reached (public member function).

byeee bestie 🫢 this page came from http://cppreference.com and it's dated 2024.06.10 β€” std::future::wait_for(3) out πŸ’…βœ¨

πŸ“¦ where this page comes from