File tree Expand file tree Collapse file tree 1 file changed +50
-1
lines changed
Expand file tree Collapse file tree 1 file changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
3030
3131
3232## 例
33+ ### 例1
3334```cpp example
3435#include <iostream>
3536#include <future>
@@ -70,11 +71,59 @@ int main()
7071* std::future_status[ link /reference/future/future_status.md]
7172* f.get()[ link /reference/future/shared_future/get.md]
7273
73- ### 出力例
74+ #### 出力例
7475```
75763
7677```
7778
79+ ### 例2
80+ ``` cpp example
81+ #include < iostream>
82+ #include < future>
83+ #include < chrono>
84+
85+ int main ()
86+ {
87+ std::promise<int > p;
88+ std::future<int > f = p.get_future();
89+ const auto ready = [ &f] {
90+ return f.wait_for(std::chrono::seconds{0}) == std::future_status::ready;
91+ };
92+
93+ // まだ値はセットされていない
94+ std::cout << std::boolalpha << ready() << std::endl;
95+
96+ p.set_value(1);
97+
98+ // 値がセットされた
99+ std::cout << std::boolalpha << ready() << std::endl;
100+
101+ f.get(); // 一度値を取り出すと共有状態が破棄される
102+
103+ // 共有状態を持たない(valid() == falseな)futureでwaitをするとstd::future_error例外
104+ try {
105+ ready();
106+ }
107+ catch(const std::future_error& e) {
108+ std::cout << e.what() << std::endl;
109+ }
110+ }
111+ ```
112+ * wait_for[ color ff0000]
113+ * std::promise[ link /reference/future/promise.md]
114+ * p.set_value[ link /reference/future/promise/set_value.md]
115+ * std::future_status[ link /reference/future/future_status.md]
116+ * f.get()[ link /reference/future/shared_future/get.md]
117+ * valid()[ link /reference/future/future/valid.md]
118+ * std::future_error[ link /reference/future/future_error.md]
119+
120+ #### 出力例
121+ ```
122+ false
123+ true
124+ std::future_error: No associated state
125+ ```
126+
78127## バージョン
79128### 言語
80129- C++11
You can’t perform that action at this time.
0 commit comments