Namespaces
Variants

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

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
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
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
void make_ready_at_thread_exit ( ArgTypes... args ) ;
(C++11 이후)

저장된 태스크를 INVOKE<R> ( f, args... ) 와 같이 호출합니다. 여기서 f 는 저장된 태스크입니다. 태스크의 반환값 또는 태스크에 의해 발생한 모든 예외는 * this 의 공유 상태에 저장됩니다.

공유 상태는 현재 스레드가 종료되고 스레드 로컬 저장 기간을 가진 모든 객체가 소멸된 후에만 준비됩니다.

목차

매개변수

args - 저장된 작업 호출 시 전달할 매개변수

반환값

(없음)

예외

std::future_error 는 다음 오류 조건에서 발생합니다:

  • 저장된 태스크가 이미 호출되었습니다. 오류 카테고리는 promise_already_satisfied 로 설정됩니다.
  • * this 에 공유 상태가 없습니다. 오류 카테고리는 no_state 로 설정됩니다.

예제

#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(); // 태스크를 즉시 실행
    auto status = state->future.wait_for(std::chrono::seconds(0));
    if (status == std::future_status::timeout)
        std::cout << "worker: future가 아직 준비되지 않았습니다\n";
    else
        std::cout << "worker: future가 준비되었습니다\n";
    std::cout << "worker: 종료\n";
}
static std::shared_ptr<ProgramState> create_state()
{
    auto state = std::make_shared<ProgramState>();
    state->task = std::packaged_task<void()>{[]
    {
        std::cout << "task: 실행됨\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가 종료되었습니다\n";
    auto status = state->future.wait_for(std::chrono::seconds(0));
    if (status == std::future_status::timeout)
        std::cout << "main: future가 아직 준비되지 않았습니다\n";
    else
        std::cout << "main: future가 준비되었습니다\n";
}

출력:

task: 실행됨
worker: future가 아직 준비되지 않았습니다
worker: 종료
main: worker가 종료되었습니다
main: future가 준비되었습니다

참고 항목

함수 실행
(public member function)