std::packaged_task<R(Args...)>::make_ready_at_thread_exit

From cppreference.com
 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
 
void make_ready_at_thread_exit( ArgTypes... args );
(since C++11)

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 thrown by it is stored in the shared state of *this.

The shared state is only made ready after the current thread exits and all objects of thread-local storage duration are destroyed.

Parameters

args - the parameters to pass on invocation of the stored task

Return value

(none)

Exceptions

std::future_error on the following error conditions:

  • The stored task has already been invoked. The error category is set to promise_already_satisfied.
  • *this has no shared state. The error category is set to no_state.

Example

#include <chrono>
#include <functional>
#include <future>
#include <iostream>
#include <memory>
#include <thread>
#include <utility>
 
struct ProgramState
{
    std::packaged_task<void()> task;
    std::future<void> future;
    std::thread worker;
};
 
static void worker(std::shared_ptr<ProgramState> state)
{
    state->task.make_ready_at_thread_exit(); // execute task right away
 
    auto status = state->future.wait_for(std::chrono::seconds(0));
    if (status == std::future_status::timeout)
        std::cout << "worker: future is not ready yet\n";
    else
        std::cout << "worker: future is ready\n";
 
    std::cout << "worker: exit\n";
}
 
static std::shared_ptr<ProgramState> create_state()
{
    auto state = std::make_shared<ProgramState>();
    state->task = std::packaged_task<void()>{[]
    {
        std::cout << "task: executed\n";
    }};
    state->future = state->task.get_future();
    state->worker = std::thread{worker, state};
    return state;
}
 
int main()
{
    auto state = create_state();
 
    state->worker.join();
    std::cout << "main: worker finished\n";
 
    auto status = state->future.wait_for(std::chrono::seconds(0));
    if (status == std::future_status::timeout)
        std::cout << "main: future is not ready yet\n";
    else
        std::cout << "main: future is ready\n";
}

Output:

task: executed
worker: future is not ready yet
worker: exit
main: worker finished
main: future is ready

See also

executes the function
(public member function)