Namespaces
Variants

std::execution:: scheduler

From cppreference.net
헤더 파일에 정의됨 <execution>
template < class Sch >

concept scheduler =
std:: derived_from <
typename std:: remove_cvref_t < Sch > :: scheduler_concept ,
scheduler_t > &&
/*queryable*/ < Sch > &&
requires ( Sch && sch )
{
{
std :: execution :: schedule ( std:: forward < Sch > ( sch ) )
} - > std :: execution :: sender ;
{
auto (
std :: execution :: get_completion_scheduler <
std :: execution :: set_value_t > (
std :: execution :: get_env (
std :: execution :: schedule (
std:: forward < Sch > ( sch ) ) ) )
} - > std:: same_as < std:: remove_cvref_t < Sch >> ;
} &&
std:: equality_comparable < std:: remove_cvref_t < Sch >> &&
std:: copy_constructible < std:: remove_cvref_t < Sch >> ;

} ;
(1) (C++26부터)
헬퍼 태그 타입
struct scheduler_t { } ;
(2) (C++26부터)

scheduler 개념은 스케줄러 인 타입들로 모델링됩니다. 즉, C++ 실행 라이브러리와 함께 동작하는 스레드 풀과 같은 실행 리소스에 대한 경량 핸들러입니다.

의미론적 요구사항

Sch 타입의 스케줄러와 Env 타입의 실행 환경이 주어졌을 때, sender_in < schedule_result_t < Sch > , Env > 가 만족된다면, /*sender-in-of*/ < schedule_result_t < Sch > , Env > 가 모델링됩니다.

스케줄러의 복사 생성자, 소멸자, 등가 비교, 또는 swap 멤버 함수는 예외를 발생시키지 않아야 합니다.

모든 해당 멤버 함수들과 스케줄러 타입의 schedule 함수는 스레드 안전해야 합니다.

두 스케줄러는 동일한 실행 리소스를 나타낼 때에만 동일합니다.

주어진 스케줄러 sch 에 대해, 표현식 get_completion_scheduler < set_value_t > ( get_env ( schedule ( sch ) ) ) sch 와 동일하게 비교됩니다.

주어진 스케줄러 sch 에 대해, 표현식 get_domain ( sch ) 이 형성 가능하다면, 표현식 get_domain ( get_env ( schedule ( sch ) ) ) 또한 형성 가능하며 동일한 타입을 가집니다.

스케줄러의 소멸자는 schedule에서 반환된 발신자 객체들에 연결된 모든 수신자들의 완료를 대기하며 차단해서는 안 됩니다 (기본 리소스는 제출된 함수 객체들의 완료를 대기하기 위한 별도의 API를 제공할 수 있습니다)

예제

std::execution::run_loop 를 위한 단순 래퍼로, 단일 전용 스레드에서 run_loop의 큐를 지속적으로 폴링합니다. 드래프트 참조 구현을 사용한 데모: https://godbolt.org/z/146fY4Y91

#include <execution>
#include <iostream>
#include <thread>
class single_thread_context
{
    std::execution::run_loop loop_{};
    std::jthread thread_;
public:
    single_thread_context()
        : thread_([this] { loop_.run(); })
    {}
    single_thread_context(single_thread_context&&) = delete;
    ~single_thread_context()
    {
        loop_.finish();
    }
    std::execution::scheduler auto get_scheduler() noexcept
    {
        return loop_.get_scheduler();
    }
};
int main()
{
    single_thread_context ctx;
    std::execution::sender auto snd =
        std::execution::schedule(ctx.get_scheduler())
        | std::execution::then([]
            {
                std::cout << "Hello world! Have an int.\n";
                return 015;
            })
        | std::execution::then([](int arg) { return arg + 42; });
    auto [i] = std::this_thread::sync_wait(snd).value();
    std::cout << "Back in the main thread, result is " << i << '\n';
}

출력:

Hello world! Have an int.
Back in the main thread, result is 55

참고 항목

주어진 스케줄러에서 실행할 태스크 그래프를 준비함
(커스터마이제이션 포인트 객체)