std::packaged_task<R(Args...)>::reset
來自 cppreference.com
< cpp | thread | packaged_task
void reset(); |
(C++11 起) | |
重置狀態,放棄之前執行的結果。構造新的共享狀態。
等價於 *this = packaged_task(std::move(f)),其中 f
是儲存的任務。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
(無)
[編輯] 異常
- 如果 *this 沒有共享狀態,則丟擲 std::future_error。錯誤條件設定為 no_state。
- 如果新共享狀態沒有足夠的記憶體,則丟擲 std::bad_alloc。
- 新
packaged_task
的移動建構函式丟擲的任何異常。
[編輯] 示例
執行此程式碼
#include <cmath> #include <future> #include <iostream> #include <thread> int main() { std::packaged_task<int(int,int)> task([](int a, int b) { return std::pow(a, b); }); std::future<int> result = task.get_future(); task(2, 9); std::cout << "2^9 = " << result.get() << '\n'; task.reset(); result = task.get_future(); std::thread task_td(std::move(task), 2, 10); task_td.join(); std::cout << "2^10 = " << result.get() << '\n'; }
輸出
2^9 = 512 2^10 = 1024