std::execution:: scheduler
|
定义于头文件
<execution>
|
||
|
template
<
class
Sch
>
concept scheduler
=
|
(1) | (C++26 起) |
|
辅助标签类型
|
||
|
struct
scheduler_t
{
}
;
|
(2) | (C++26 起) |
概念
scheduler
通过作为
schedulers
的类型进行建模,即与 C++ 执行库协同工作的轻量级执行资源句柄(如线程池)。
语义要求
给定类型为
Sch
的调度器和类型为
Env
的执行环境,若满足
sender_in
<
schedule_result_t
<
Sch
>
, Env
>
条件,则建立
/*sender-in-of*/
<
schedule_result_t
<
Sch
>
, Env
>
模型。
调度器的拷贝构造函数、析构函数、相等比较或交换成员函数必须为不抛出异常的。
所有这些成员函数以及调度器类型的
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
参见
|
(C++26)
|
在给定调度器上准备执行任务图
(定制点对象) |