std::promise<R>::set_value_at_thread_exit
來自 cppreference.com
主模板 |
||
void set_value_at_thread_exit( const R& value ); |
(1) | (C++11 起) |
void set_value_at_thread_exit( R&& value ); |
(2) | (C++11 起) |
std::promise<R&> 特化 |
||
void set_value_at_thread_exit( R& value ); |
(3) | (C++11 起) |
std::promise<void> 特化 |
||
void set_value_at_thread_exit(); |
(4) | (C++11 起) |
儲存 value 到共享狀態,但不會立即使狀態就緒。狀態將在當前執行緒退出時(在所有具有執行緒區域性儲存期的變數被銷燬後)就緒。
該操作的行為如同 set_value、set_exception、set_value_at_thread_exit
和 set_exception_at_thread_exit 在更新 promise 物件時獲取與該 promise 物件關聯的單個互斥體。
此函式的呼叫不會與對 get_future 的呼叫引入資料競爭(因此它們之間不需要同步)。
目錄 |
[編輯] 引數
value | - | 要儲存到共享狀態的值 |
[編輯] 返回值
(無)
[編輯] 異常
在以下條件下丟擲 std::future_error:
- *this 沒有共享狀態。錯誤碼被設定為 no_state。
- 共享狀態已儲存值或異常。錯誤碼被設定為 promise_already_satisfied。
此外,
1) 複製型別為
R
的物件所選用的建構函式丟擲的任何異常。2) 移動型別為
R
的物件所選用的建構函式丟擲的任何異常。[編輯] 示例
執行此程式碼
#include <future> #include <iostream> #include <thread> int main() { using namespace std::chrono_literals; std::promise<int> p; std::future<int> f = p.get_future(); std::thread([&p] { std::this_thread::sleep_for(1s); p.set_value_at_thread_exit(9); }).detach(); std::cout << "Waiting... " << std::flush; f.wait(); std::cout << "Done!\nResult is: " << f.get() << '\n'; }
輸出
Waiting... Done! Result is: 9
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2098 | C++11 | 未明確應丟擲哪些異常 | 已明確 |
[編輯] 參閱
將結果設定為特定值 (public 成員函式) | |
將結果設定為指示異常,僅線上程退出時傳送通知 (public 成員函式) |