std::packag..._thread_exit(3)
π NAME π
okay bestie, you're on the std::packag..._thread_exit(3) page from the C++ Standard Libary manual β yes, that's the manual name, and we're keeping it cute. The actual star of the show is:
std::packaged_task::make_ready_at_thread_exitβstd::packaged_task::make_ready_at_thread_exit
πΈ Synopsis πΈ
void make_ready_at_thread_exit( ArgTypes... args ); (since C++11)
So what's the tea? It calls the stored task as if by INVOKE<R>(f, args...), where f is the stored task. The return value of the task β or any exception it throws β gets stored in the shared state of *this. π
But here's the dramatic part: the shared state is only made ready after the current thread exits and all objects of thread-local storage duration are destroyed. It's giving "wait for the glow-up to finish" energy, no cap. β¨
π Parameters π
argsβ the parameters to pass on invocation of the stored task.
π¦ Return value π¦
(none) β literally nothing, bestie.
π Exceptions π
std::future_error is the vibe on the following error conditions:
- The stored task has already been invoked. The error category is set to
promise_already_satisfied. *thishas no shared state. The error category is set tono_state.
β¨ Example β¨
// Run this code
#include <chrono>
#include <functional>
#include <future>
#include <iostream>
#include <thread>
#include <utility>
void worker(std::future<void>& output)
{
std::packaged_task<void(bool&)> my_task{[](bool& done) { done =
true; }};
auto result = my_task.get_future();
bool done = false;
my_task.make_ready_at_thread_exit(done); // execute task right away
std::cout << "worker: done = " << std::boolalpha << done <<
std::endl;
auto status = result.wait_for(std::chrono::seconds(0));
if (status == std::future_status::timeout)
std::cout << "worker: result is not ready yet" << std::endl;
output = std::move(result);
}
int main()
{
std::future<void> result;
std::thread{worker, std::ref(result)}.join();
auto status = result.wait_for(std::chrono::seconds(0));
if (status == std::future_status::ready)
std::cout << "main: result is ready" << std::endl;
}
Output:
worker: done = true
worker: result is not ready yet
main: result is ready
π See also π
operator()β executes the function (public member function).
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::packag..._thread_exit(3) β 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