std::execution::scheduler
定義於標頭檔案 <execution> |
||
template< class Sch > concept scheduler = |
(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> 被建模。
排程器的複製建構函式、解構函式、相等比較或交換成員函式不得丟擲異常。
所有這些成員函式以及排程器型別的 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) |
準備一個任務圖,以便在給定的排程器上執行 (自定義點物件) |