std::shared_future::wait(3)
π std::shared_future::wait β C++ Standard Library, bestie edition π
okay so you're on the std::shared_future::wait page, which lives in the C++ Standard Library manual. sit down, hydrate, and let's get into it π«Ά
β¨ NAME β¨
std::shared_future::wait β literally just std::shared_future::wait, no notes, that's the whole name π
πΈ Synopsis πΈ
void wait() const; (since C++11)
Blocks until the result becomes available. After the call, valid() == true β¨
β οΈ The behavior is undefined if valid() == false before the call to this function. like. undefined undefined. don't do it π
π¦ Parameters π¦
(none) β she takes zero arguments, high maintenance queen π
β Return value β
(none) β she gives you nothing back, and honestly? iconic.
π Exceptions π
May throw implementation-defined exceptions. vague, mysterious, a little bit main character energy π
π Notes π
- implementations are encouraged to detect the case where
valid() == falsebefore the call, and to throw astd::future_errorwith an error condition ofstd::future_errc::no_state. so like, they should catch you being messy, but no cap, it's just "encouraged" π
π₯ Callingwaiton the samestd::shared_futurefrom multiple threads is not safe. the intended use is that each thread that waits on the same shared state has its own copy of astd::shared_future. one future per thread, bestie. share the state, not the object π
π Example π
// Run this code
#include <chrono>
#include <future>
#include <iostream>
#include <thread>
int fib(int n)
{
if (n < 3)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
std::shared_future<int> f1 = std::async(std::launch::async, []() { return fib(40); });
std::shared_future<int> f2 = std::async(std::launch::async, []() { return fib(43); });
std::cout << "waiting... " << std::flush;
const auto start = std::chrono::system_clock::now();
f1.wait();
f2.wait();
const auto diff = std::chrono::system_clock::now() - start;
std::cout << std::chrono::duration<double>(diff).count() << " seconds\n";
std::cout << "f1: " << f1.get() << '\n';
std::cout << "f2: " << f2.get() << '\n';
}
π§ Possible output π§
waiting... 1.61803 seconds
f1: 102334155
f2: 433494437
π See also π
wait_forβ waits for the result, returns if it is not available for the specified timeout duration (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)
k bye bestie, go wait on your futures responsibly π¦β¨ β this lil page is from http://cppreference.com, dated 2024.06.10, and it's the std::shared_future::wait(3) manual page. mwah ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms