|
| 1 | +# run_loop |
| 2 | +* execution[meta header] |
| 3 | +* class[meta id-type] |
| 4 | +* std::execution[meta namespace] |
| 5 | +* cpp26[meta cpp] |
| 6 | + |
| 7 | +```cpp |
| 8 | +namespace std::execition { |
| 9 | + class run_loop; |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +## 概要 |
| 14 | +`run_loop`は、実行制御ライブラリの作業を[スケジュール](schedule.md)可能な実行リソース(execution resource)である。 |
| 15 | +
|
| 16 | +内部的にスレッドセーフなFIFO (first-in first-out) 作業キューを保持する。 |
| 17 | +[`run`メンバ関数](run_loop/run.md.nolink)はキューから作業を取り出し、同関数を呼び出したスレッド上のループで実行する。 |
| 18 | +
|
| 19 | +`run_loop`インスタンスの動作説明のため、下記の説明用メンバ変数を持つ。 |
| 20 | +
|
| 21 | +- `count` : FIFOキューが保持する作業の個数 |
| 22 | +- `state` : 開始(starting)/実行中(running)/完了中(finishing)/完了済み(finished) いずれかのインスタンス状態 |
| 23 | +
|
| 24 | +
|
| 25 | +## メンバ関数 |
| 26 | +
|
| 27 | +| 名前 | 説明 | 対応バージョン | |
| 28 | +|------|------|-------| |
| 29 | +| [`(constructor)`](run_loop/op_constructor.md) | コンストラクタ | C++26 | |
| 30 | +| [`(destructor)`](run_loop/op_destructor.md) | デストラクタ | C++26 | |
| 31 | +| [`get_scheduler`](run_loop/get_scheduler.md) | Scheduler取得 | C++26 | |
| 32 | +| [`run`](run_loop/run.md.nolink) | ループ実行を開始 | C++26 | |
| 33 | +| [`finish`](run_loop/finish.md.nolink) | ループ実行を終了 | C++26 | |
| 34 | +
|
| 35 | +## 説明専用のメンバ型 |
| 36 | +
|
| 37 | +| 名前 | 説明 | 対応バージョン | |
| 38 | +|------|------|-------| |
| 39 | +| [`run-loop-scheduler`](run_loop/run-loop-scheduler.md) | 説明専用クラス | C++26 | |
| 40 | +| [`run-loop-sender`](run_loop/run-loop-sender.md) | 説明専用クラス | C++26 | |
| 41 | +| [`run-loop-opstate`](run_loop/run-loop-opstate.md.nolink) | 説明専用クラス | C++26 | |
| 42 | +
|
| 43 | +
|
| 44 | +## 例 |
| 45 | +```cpp example |
| 46 | +#include <execution> |
| 47 | +namespace ex = std::execution; |
| 48 | +
|
| 49 | +struct MyReceiver { |
| 50 | + using receiver_concept = ex::receiver_t; |
| 51 | +
|
| 52 | + void set_value() noexcept |
| 53 | + { std::println("value"); } |
| 54 | + void set_error(std::exception_ptr) noexcept |
| 55 | + { std::println("error"); } |
| 56 | + void set_stopped() noexcept |
| 57 | + { std::println("stopped"); } |
| 58 | +}; |
| 59 | +
|
| 60 | +
|
| 61 | +int main() |
| 62 | +{ |
| 63 | + // run_loopのスケジュールSenderを取得 |
| 64 | + ex::run_loop loop; |
| 65 | + ex::scheduler auto sch = loop.get_scheduler(); |
| 66 | + // state:開始(starting) |
| 67 | +
|
| 68 | + ex::sender auto sndr = ex::schedule(sch); |
| 69 | + ex::receiver auto rcvr = MyReceiver{}; |
| 70 | + ex::operation_state auto op = ex::connect(sndr, rcvr); |
| 71 | + // キューに作業を1つ追加 |
| 72 | + ex::start(op); |
| 73 | +
|
| 74 | + // stateを完了中(finished)へ遷移 |
| 75 | + loop.finish(); |
| 76 | +
|
| 77 | + // キュー上の作業を全て処理 |
| 78 | + loop.run(); |
| 79 | + // state:完了済み(finished) |
| 80 | +} |
| 81 | +``` |
| 82 | +* ex::run_loop[color ff0000] |
| 83 | +* ex::scheduler[link scheduler.md] |
| 84 | +* ex::sender[link sender.md] |
| 85 | +* ex::schedule[link schedule.md] |
| 86 | +* ex::receiver[link receiver.md] |
| 87 | +* ex::receiver_t[link receiver.md] |
| 88 | +* ex::operation_state[link operation_state.md] |
| 89 | +* ex::connect[link connect.md] |
| 90 | +* ex::start[link start.md] |
| 91 | +* get_scheduler()[link run_loop/get_scheduler.md] |
| 92 | +* finish()[link run_loop/finish.md.nolink] |
| 93 | +* run()[link run_loop/run.md.nolink] |
| 94 | + |
| 95 | +### 出力 |
| 96 | +``` |
| 97 | +value |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +## バージョン |
| 102 | +### 言語 |
| 103 | +- C++26 |
| 104 | + |
| 105 | +### 処理系 |
| 106 | +- [Clang](/implementation.md#clang): ?? |
| 107 | +- [GCC](/implementation.md#gcc): ?? |
| 108 | +- [ICC](/implementation.md#icc): ?? |
| 109 | +- [Visual C++](/implementation.md#visual_cpp): ?? |
| 110 | + |
| 111 | + |
| 112 | +## 関連項目 |
| 113 | +- [`execution::schedule`](schedule.md) |
| 114 | + |
| 115 | + |
| 116 | +## 参照 |
| 117 | +- [P2300R10 `std::execution`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html) |
0 commit comments