std::future::wait (3) Linux Manual Page
std::future<T>::wait – std::future<T>::wait
Synopsis
void wait() const;
(since C++ 11)
Blocks until the result becomes available. valid() == true after the call.
The behavior is undefined if valid()== false before the call to this function.
Parameters
(none)
Return value
(none)
Exceptions
(none)
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
#include <iostream>
#include <future>
#include <thread>
int fib(int n)
{
if (n < 3)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
std::future<int> f1 = std::async(std::launch::async, []() {
return fib(20);
});
std::future<int> f2 = std::async(std::launch::async, []() {
return fib(25);
});
std::cout << "waiting...\n";
f1.wait();
f2.wait();
std::cout << "f1: " << f1.get() << '\n';
std::cout << "f2: " << f2.get() << '\n';
}
Output:
See also
wait_for (public member function)
wait_until (public member function)
