<future>
函式
async
future_category
make_error_code
make_error_condition
swap
|
async
代表「非同步提供者」。
template <class Fn, class... ArgTypes>
future<typename result_of<Fn(ArgTypes...)>::type>
async(Fn&& fn, ArgTypes&&... args);
template <class Fn, class... ArgTypes>
future<typename result_of<Fn(ArgTypes...)>::type>
async(launch policy, Fn&& fn, ArgTypes&&... args);
參數
政策
launch
值。
備註
縮寫的定義:
縮寫 | 描述 |
---|---|
dfn |
呼叫 decay_copy(forward<Fn>(fn)) 的結果。 |
dargs |
呼叫 decay_copy(forward<ArgsTypes>(args...)) 的結果。 |
Ty |
result_of<Fn(ArgTypes...)>::type 型別。 |
第一個範本函式會傳回 async(launch::any, fn, args...)
。
第二個 future<Ty>
函式會傳回物件,其 相關聯的非同步狀態 會與 的值和 dargs
執行緒物件一 dfn
起保存結果,以管理個別的執行執行緒。
除非 decay<Fn>::type
是 launch 以外的類型,否則第二個函式不會參與多載解析。
C++ 標準指出,如果 policy 是 launch::async,則函式會建立新的執行緒。 不過,Microsoft 實作目前不符合規範。 它會從 Windows ThreadPool 取得其執行緒,在某些情況下可能會提供回收的執行緒,而不是新的執行緒。 這表示原則 launch::async
會實作為 launch::async|launch::deferred
。 ThreadPool 型實作的另一個含意是,不保證執行緒區域變數會線上程完成時終結。 如果執行緒已回收並提供給新的呼叫 async
,舊的變數仍會存在。 建議您不要搭配 async
使用執行緒區域變數。
如果 policy 為 launch::deferred
,則函式會將其相關聯的非同步狀態標示為保留 延後函 式並傳回 。 第一次呼叫任何等待相關非同步狀態變成就緒的非計時函式時,實際上會透過評估 INVOKE(dfn, dargs..., Ty)
來呼叫 deferred 函式。
在所有情況下,在評估 INVOKE(dfn, dargs..., Ty)
完成之前,物件相關聯的非同步狀態 future
不會設定為 就緒 ,無論是擲回例外狀況還是正常傳回。 如果擲回例外狀況,相關非同步狀態的結果就會是例外狀況,否則會是評估所傳回的任何值。
注意
對於附加至以 開頭 std::async
之工作的最後 shared_future
一 future
個 ,解構函式會在工作尚未完成時封鎖;也就是說,如果此執行緒尚未呼叫 .get()
或 .wait()
,則會封鎖工作仍在執行中。 如果將從 std::async
取得的 future
移到區域範圍外,其他使用它的程式碼必須知悉其解構函式可能進行封鎖來讓共用狀態變成就緒。
虛擬函 INVOKE
式定義于 中 <functional>
。
Microsoft 特定的
以非同步方式執行傳遞的函式時,它會在 Windows 執行緒集區上執行;請參閱 執行緒集區 。 平行線程數目限制為執行緒集區預設值 (目前為 500 個)。 在硬體上並存執行的執行緒數目目前受限於進程處理器群組中的邏輯處理器數目,因此實際上限制為 64:請參閱 處理器群組 。
future_category
傳回對 error_category 物件的參考,此物件會描述 future
物件之相關錯誤的特性。
const error_category& future_category() noexcept;
make_error_code
建立 error_code 連同描述 future 錯誤特性的 error_category 物件。
inline error_code make_error_code(future_errc Errno) noexcept;
參數
Errno
future_errc 值,可識別所回報的錯誤。
傳回值
error_code(static_cast<int>(Errno), future_category());
make_error_condition
建立 error_condition 連同描述 future 錯誤特性的 error_category 物件。
inline error_condition make_error_condition(future_errc Errno) noexcept;
參數
Errno
future_errc 值,可識別所回報的錯誤。
傳回值
error_condition(static_cast<int>(Errno), future_category());
swap
將一個 promise
物件的「相關非同步狀態」與另一個物件的該狀態交換。
template <class Ty>
void swap(promise<Ty>& Left, promise<Ty>& Right) noexcept;
template <class Ty, class... ArgTypes>
void swap(packaged_task<Ty(ArgTypes...)>& Left, packaged_task<Ty(ArgTypes...)>& Right) noexcept;
參數
Left
左 promise
物件。
Right
右 promise
物件。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應