public member function
<future>

std::future::wait_for

template <class Rep, class Period>
  future_status wait_for (const chrono::duration<Rep,Period>& rel_time) const;
Wait for ready during time span
Waits for the shared state to be ready for up to the time specified by rel_time.

If the shared state is not yet ready (i.e., the provider has not yet set its value or exception), the function blocks the calling thread and waits until it is ready or until rel_time has elapsed, whichever happens first.

When the function returns because its shared state is made ready, the value or exception set on the shared state is not read, but all visible side effects are synchronized between the point the provider makes the shared state ready and the return of this function.

If the shared state contains a deferred function (such as future objects returned by async), the function does not block, returning immediately with a value of future_status::deferred.

Parameters

rel_time
The time span after which the function returns resuming execution of the calling thread.
Note that multi-threading management operations may cause certain delays beyond this.
duration is an object that represents a specific relative time.

Return value

A value of type future_status indicating what caused the function to return:
valuedescription
future_status::readyThe shared state is ready: the producer has set a value or exception.
future_status::timeoutThe function waited for rel_time without the shared state becoming ready.
future_status::deferredThe shared state contains a deferred function.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// future::wait_for
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // std::chrono::milliseconds

// a non-optimized way of checking for prime numbers:
bool is_prime (int x) {
  for (int i=2; i<x; ++i) if (x%i==0) return false;
  return true;
}

int main ()
{
  // call function asynchronously:
  std::future<bool> fut = std::async (is_prime,700020007); 

  // do something while waiting for function to set future:
  std::cout << "checking, please wait";
  std::chrono::milliseconds span (100);
  while (fut.wait_for(span)==std::future_status::timeout)
    std::cout << '.';

  bool x = fut.get();

  std::cout << "\n700020007 " << (x?"is":"is not") << " prime.\n";

  return 0;
}


Possible output (may take more or less time):

checking, please wait..........................................
700020007 is prime.

Data races

The future object is accessed.
The shared state is accessed as an atomic operation (causing no data races).

Exception safety

Calling this member function on a future object that is not valid, produces undefined behavior (although library implementations may detect this and throw future_error with a no_state error condition, offering a strong guarantee).

It may throw if an operation related to rel_time throws (note that operations on duration types provided in <chrono>, such as seconds, never throw).

See also